Skip to content

Commit

Permalink
Change design for Size table editing
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyNefyodov committed Jun 27, 2024
1 parent 3777450 commit dad39f6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ public void RegisterMenu(ContextMenu contextMenu)
}
});

contextMenu.AddMenuItem("ShowMenuItem")
contextMenu.AddMenuItem("EditMenuItem")
.SetHeader("Edit table")
.SetAvailability(manager.GetAllSizeTableNames().Count > 0)
.SetCommand(manager, async sizeTable =>
.SetCommand(manager, async _ =>
{
var context = (ISnoopViewModel) contextMenu.DataContext;
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,12 @@ public sealed class FamilySizeTableEditDialogViewModel : DataTable
public FamilySizeTableEditDialogViewModel(FamilySizeTableManager manager, string tableName)
{
_manager = manager;
DeleteRowCommand = new RelayCommand<DataRow>(DeleteRow);
DuplicateRowCommand = new RelayCommand<DataRow>(DuplicateRow);
_tableName = tableName;
var table = manager.GetSizeTable(tableName);
CreateColumns(table);
WriteRows(table);
}

public RelayCommand<DataRow> DeleteRowCommand { get; }
public RelayCommand<DataRow> DuplicateRowCommand { get; }

public FamilySizeTableEditDialogViewModel(FamilySizeTable table)
{
CreateColumns(table);
Expand All @@ -68,19 +63,25 @@ private void CreateColumns(FamilySizeTable table)
for (var i = 0; i < table.NumberOfColumns; i++)
{
var header = table.GetColumnHeader(i);
var specId = header.GetSpecTypeId();
var typeId = table.GetColumnHeader(i).GetUnitTypeId();
var headerName = table.GetColumnHeader(i).Name;

var columnName = headerName;
if (!specId.Empty())

if (i == 0)
{
columnName = $"{columnName}##{specId.ToSpecLabel().ToLowerInvariant()}";
columnName = "Column 1";
}

if (!typeId.Empty())
else
{
columnName = $"{columnName}##{typeId.ToUnitLabel().ToLowerInvariant()}";
var specId = header.GetSpecTypeId();
var typeId = table.GetColumnHeader(i).GetUnitTypeId();

if (!specId.Empty())
{
columnName = $"{columnName}##{specId.ToSpecLabel().ToLowerInvariant()}";
}

columnName = !typeId.Empty() ? $"{columnName}##{typeId.ToUnitLabel().ToLowerInvariant()}" : $"{columnName}##other##";

}

Columns.Add(new DataColumn(columnName, typeof(string)));
Expand Down Expand Up @@ -133,12 +134,12 @@ public void SaveData()
});
}

private void DeleteRow(DataRow row)
public void DeleteRow(DataRow row)
{
Rows.Remove(row);
}

private void DuplicateRow(DataRow row)
public void DuplicateRow(DataRow row)
{
var index = Rows.IndexOf(row);
var newRow = NewRow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void Export()
Filter = "CSV files (*.csv)|*.csv",
FileName = SelectedTable,
RestoreDirectory = true,
Title = "Save family size table",
Title = "Save family size table"
};
if (saveFileDialog.ShowDialog() == false) return;

Expand Down
46 changes: 10 additions & 36 deletions source/RevitLookup/Views/Dialogs/FamilySizeTableEditDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,21 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:dialogs="clr-namespace:RevitLookup.ViewModels.Dialogs"
xmlns:rl="http://revitlookup.com/xaml"
xmlns:data="clr-namespace:System.Data;assembly=System.Data"
xmlns:markup="clr-namespace:RevitLookup.Views.Markup"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance dialogs:FamilySizeTableEditDialogViewModel}">
<Grid.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<markup:MenusDictionary />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Grid.Resources>
<DataGrid
IsReadOnly="False"
CanUserAddRows="False"
HeadersVisibility="Column"
CanUserReorderColumns="False"
ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTemplateColumn IsReadOnly="True" Header="Actions">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<rl:Button
Padding="2"
Margin="4 2"
ToolTip="Delete row"
CommandParameter="{Binding Path=DataContext.(data:DataRowView.Row), RelativeSource = {RelativeSource Self}}"
Command="{Binding DataContext.DeleteRowCommand,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}">
<rl:Button.Content>
<rl:SymbolIcon Symbol="Delete24" />
</rl:Button.Content>
</rl:Button>
<rl:Button
Padding="2"
Margin="4 2"
ToolTip="Duplicate row"
CommandParameter="{Binding Path=DataContext.(data:DataRowView.Row), RelativeSource = {RelativeSource Self}}"
Command="{Binding DataContext.DuplicateRowCommand,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}">
<rl:Button.Content>
<rl:SymbolIcon Symbol="Copy24" />
</rl:Button.Content>
</rl:Button>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
LoadingRow="OnGridRowLoading"
ItemsSource="{Binding}" />
</Grid>
38 changes: 38 additions & 0 deletions source/RevitLookup/Views/Dialogs/FamilySizeTableEditDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.

using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using RevitLookup.ViewModels.Dialogs;
using RevitLookup.Views.Extensions;
using Wpf.Ui;
using Wpf.Ui.Controls;

Expand Down Expand Up @@ -68,4 +72,38 @@ public async Task ShowAsync()
_viewModel.SaveData();
}
}

private void CreateGridRowContextMenu(DataRow dataRow, FrameworkElement dataGridRow)
{
var contextMenu = new ContextMenu
{
Resources = Resources,
PlacementTarget = dataGridRow,
DataContext = _viewModel
};

dataGridRow.ContextMenu = contextMenu;
contextMenu.AddMenuItem("CopyMenuItem")
.SetHeader("Duplicate row")
.SetCommand(dataRow, _ => _viewModel.DuplicateRow(dataRow))
.SetShortcut(ModifierKeys.Control, Key.C);

contextMenu.AddMenuItem("DeleteMenuItem")
.SetHeader("Delete row")
.SetCommand(dataRow, _ => _viewModel.DeleteRow(dataRow))
.SetShortcut(ModifierKeys.Control, Key.Delete);
}

private void OnGridRowLoading(object sender, DataGridRowEventArgs args)
{
var row = args.Row;
row.Loaded += OnGridRowLoaded;
}

private void OnGridRowLoaded(object sender, RoutedEventArgs args)
{
var element = (FrameworkElement) sender;
var context = (DataRowView) element.DataContext;
CreateGridRowContextMenu(context.Row, element);
}
}

0 comments on commit dad39f6

Please sign in to comment.