Skip to content

Commit 13b2483

Browse files
Refactoring DataGridAssist input handling (#2824)
* Refactoring DataGridAssist mouse handling Rather than handling the tunneling event and "pre-processing", it now handles the (already handled - by the cell) bubbling event to perform additional actions. * Added template column example Fixed issue where it would not switch to edit on click Co-authored-by: Kevin Bost <[email protected]>
1 parent 6a08d55 commit 13b2483

File tree

4 files changed

+316
-304
lines changed

4 files changed

+316
-304
lines changed

MainDemo.Wpf/DataGrids.xaml

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,39 @@
1111
d:DesignHeight="300"
1212
d:DesignWidth="600"
1313
d:DataContext="{d:DesignInstance domain:ListsAndGridsViewModel}">
14-
14+
1515
<UserControl.Resources>
1616
<ResourceDictionary>
1717
<ResourceDictionary.MergedDictionaries>
1818
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.DataGrid.xaml" />
1919
</ResourceDictionary.MergedDictionaries>
2020
</ResourceDictionary>
2121
</UserControl.Resources>
22-
22+
2323
<StackPanel>
24-
<TextBlock
25-
Style="{StaticResource MaterialDesignHeadline5TextBlock}"
26-
Text="Custom Columns"/>
27-
24+
<StackPanel Orientation="Horizontal">
25+
<TextBlock
26+
Style="{StaticResource MaterialDesignHeadline5TextBlock}"
27+
Text="Custom Columns"/>
28+
<TextBlock
29+
Style="{StaticResource MaterialDesignBody1TextBlock}" VerticalAlignment="Center"
30+
Margin="25,0,10,0"
31+
Text="SelectionUnit:"/>
32+
<ComboBox x:Name="selectionUnitComboBox" ItemsSource="{Binding SelectionUnits}" SelectedIndex="0" VerticalAlignment="Center" />
33+
</StackPanel>
34+
2835
<smtx:XamlDisplay UniqueKey="grids_1">
2936
<DataGrid
3037
ItemsSource="{Binding Items1}"
3138
CanUserAddRows="False" AutoGenerateColumns="False"
32-
HeadersVisibility="All">
39+
HeadersVisibility="All"
40+
SelectionUnit="{Binding ElementName=selectionUnitComboBox, Path=SelectedValue}">
3341
<DataGrid.Resources>
3442
<domain:BindingProxy
3543
x:Key="DataContextProxy"
3644
Data="{Binding}" />
3745
</DataGrid.Resources>
38-
46+
3947
<DataGrid.Columns>
4048
<DataGridCheckBoxColumn
4149
Binding="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}"
@@ -47,20 +55,20 @@
4755
IsChecked="{Binding Data.IsAllItems1Selected, Source={StaticResource DataContextProxy}}"/>
4856
</Border>
4957
</DataGridCheckBoxColumn.Header>
50-
58+
5159
<DataGridCheckBoxColumn.HeaderStyle>
5260
<Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource MaterialDesignDataGridColumnHeader}">
5361
<Setter Property="HorizontalContentAlignment" Value="Center" />
5462
</Style>
5563
</DataGridCheckBoxColumn.HeaderStyle>
5664
</DataGridCheckBoxColumn>
57-
65+
5866
<DataGridTextColumn
5967
Binding="{Binding Code}"
6068
Header="Code"
6169
ElementStyle="{StaticResource MaterialDesignDataGridTextColumnStyle}"
6270
EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}"/>
63-
71+
6472
<!-- if you want to use the pop up style (MaterialDesignDataGridTextColumnPopupEditingStyle), you must use MaterialDataGridTextColumn -->
6573
<materialDesign:DataGridTextColumn
6674
Header="Name"
@@ -74,7 +82,7 @@
7482
</Binding>
7583
</materialDesign:DataGridTextColumn.Binding>
7684
</materialDesign:DataGridTextColumn>
77-
85+
7886
<!-- set a max length to get an indicator in the editor -->
7987
<DataGridTextColumn
8088
Header="Description"
@@ -88,7 +96,7 @@
8896
</Binding>
8997
</DataGridTextColumn.Binding>
9098
</DataGridTextColumn>
91-
99+
92100
<materialDesign:DataGridTextColumn
93101
Binding="{Binding Numeric}"
94102
Header="Number with long header"
@@ -109,7 +117,7 @@
109117
</Setter>
110118
</Style>
111119
</DataGridTextColumn.HeaderStyle>
112-
120+
113121
<DataGridTextColumn.ElementStyle>
114122
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource MaterialDesignDataGridTextColumnStyle}">
115123
<Setter Property="HorizontalAlignment" Value="Right" />
@@ -134,9 +142,20 @@
134142
<materialDesign:DataGridComboBoxColumn
135143
Header="ComboBox with long list"
136144
SelectedValueBinding="{Binding Files}"
137-
ItemsSourceBinding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.Files}">
145+
ItemsSourceBinding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.Files}" />
138146

139-
</materialDesign:DataGridComboBoxColumn>
147+
<DataGridTemplateColumn Header="Template Column">
148+
<DataGridTemplateColumn.CellTemplate>
149+
<DataTemplate DataType="{x:Type domain:SelectableViewModel}">
150+
<TextBlock Text="{Binding Name}" FontStyle="Italic" FontSize="14" />
151+
</DataTemplate>
152+
</DataGridTemplateColumn.CellTemplate>
153+
<DataGridTemplateColumn.CellEditingTemplate>
154+
<DataTemplate DataType="{x:Type domain:SelectableViewModel}">
155+
<TextBox Text="{Binding Name}" Foreground="{DynamicResource SecondaryHueMidBrush}" />
156+
</DataTemplate>
157+
</DataGridTemplateColumn.CellEditingTemplate>
158+
</DataGridTemplateColumn>
140159
</DataGrid.Columns>
141160
</DataGrid>
142161
</smtx:XamlDisplay>
@@ -145,24 +164,24 @@
145164
<materialDesign:PackIcon Kind="Information" Margin="0 0 5 0"/>
146165
<TextBlock>DataGridComboBoxColumns are virtualized by default in the library</TextBlock>
147166
</StackPanel>
148-
167+
149168
<TextBlock
150169
Style="{StaticResource MaterialDesignHeadline5TextBlock}"
151170
Text="Auto Generated Columns"
152171
Margin="0 24 0 0"/>
153-
172+
154173
<smtx:XamlDisplay UniqueKey="grids_2">
155174
<DataGrid
156175
ItemsSource="{Binding Items2}"
157176
CanUserAddRows="False"
158177
SelectionUnit="Cell"
159178
SelectionMode="Extended" />
160179
</smtx:XamlDisplay>
161-
180+
162181
<TextBlock
163182
Style="{StaticResource MaterialDesignHeadline6TextBlock}"
164183
Text="Custom Padding" Margin="0 24 0 0"/>
165-
184+
166185
<smtx:XamlDisplay UniqueKey="grids_3">
167186
<DataGrid
168187
ItemsSource="{Binding Items3}"

MainDemo.Wpf/Domain/ListsAndGridsViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.Collections.ObjectModel;
33
using System.IO;
44
using System.Linq;
@@ -89,5 +89,6 @@ private static ObservableCollection<SelectableViewModel> CreateData()
8989

9090
public IList<string> Files { get; }
9191

92+
public IEnumerable<DataGridSelectionUnit> SelectionUnits => new[] { DataGridSelectionUnit.FullRow, DataGridSelectionUnit.Cell, DataGridSelectionUnit.CellOrRowHeader };
9293
}
9394
}
Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,54 @@
1-
namespace MaterialDesignDemo.Domain
1+
namespace MaterialDesignDemo.Domain;
2+
3+
public class SelectableViewModel : ViewModelBase
24
{
3-
public class SelectableViewModel : ViewModelBase
5+
private bool _isSelected;
6+
private string? _name;
7+
private string? _description;
8+
private char _code;
9+
private double _numeric;
10+
private string? _food;
11+
private string? _files;
12+
13+
public bool IsSelected
14+
{
15+
get => _isSelected;
16+
set => SetProperty(ref _isSelected, value);
17+
}
18+
19+
public char Code
20+
{
21+
get => _code;
22+
set => SetProperty(ref _code, value);
23+
}
24+
25+
public string? Name
26+
{
27+
get => _name;
28+
set => SetProperty(ref _name, value);
29+
}
30+
31+
public string? Description
32+
{
33+
get => _description;
34+
set => SetProperty(ref _description, value);
35+
}
36+
37+
public double Numeric
38+
{
39+
get => _numeric;
40+
set => SetProperty(ref _numeric, value);
41+
}
42+
43+
public string? Food
44+
{
45+
get => _food;
46+
set => SetProperty(ref _food, value);
47+
}
48+
49+
public string? Files
450
{
5-
private bool _isSelected;
6-
private string? _name;
7-
private string? _description;
8-
private char _code;
9-
private double _numeric;
10-
private string? _food;
11-
private string? _files;
12-
13-
public bool IsSelected
14-
{
15-
get => _isSelected;
16-
set => SetProperty(ref _isSelected, value);
17-
}
18-
19-
public char Code
20-
{
21-
get => _code;
22-
set => SetProperty(ref _code, value);
23-
}
24-
25-
public string? Name
26-
{
27-
get => _name;
28-
set => SetProperty(ref _name, value);
29-
}
30-
31-
public string? Description
32-
{
33-
get => _description;
34-
set => SetProperty(ref _description, value);
35-
}
36-
37-
public double Numeric
38-
{
39-
get => _numeric;
40-
set => SetProperty(ref _numeric, value);
41-
}
42-
43-
public string? Food
44-
{
45-
get => _food;
46-
set => SetProperty(ref _food, value);
47-
}
48-
49-
public string? Files
50-
{
51-
get => _files;
52-
set => SetProperty(ref _files, value);
53-
}
51+
get => _files;
52+
set => SetProperty(ref _files, value);
5453
}
5554
}

0 commit comments

Comments
 (0)