Skip to content

Commit ffb48f0

Browse files
greuelpiratKeboo
andauthored
Revised DataGrid-Text-Cell (#1787)
* Fixed missing style on auto generated DataGridTextColumn (#1779) * Fixed various warnings for Grids.xaml and List.xaml (#1779) * Added AutoGeneratedTextStyle for ValidationErrorTemplate on TextBlock (#1779) * Changed header styles, moved margin (#1779) * Removed custom padding from first grid (#1779) * Using List1 for first grid, List2 for second (#1779) * Added NotEmptyValidationRule to Name column for testing purposes (#1779) * MaterialDesignDataGridTextColumnEditingStyle uses ValidationErrorBrush on Validation.HasError (#1779) * Own Validation-Error-Implementation for MaterialDesignDataGridTextColumnPopupEditingStyle (#1779) * Fixed position difference between view and edit style for text column (#1779) * Remove default error template for MaterialDesignDataGridCell (#1779) * Adding sample of validation with default DataGridTextColumn Renamed Grids, to DataGrids in demo app. Co-authored-by: Kevin Bost <[email protected]>
1 parent 5d2479e commit ffb48f0

File tree

8 files changed

+164
-82
lines changed

8 files changed

+164
-82
lines changed

MainDemo.Wpf/Grids.xaml renamed to MainDemo.Wpf/DataGrids.xaml

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<UserControl x:Class="MaterialDesignColors.WpfExample.Grids"
1+
<UserControl x:Class="MaterialDesignColors.WpfExample.DataGrids"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
@@ -7,7 +7,7 @@
77
xmlns:smtx="clr-namespace:ShowMeTheXAML;assembly=ShowMeTheXAML"
88
xmlns:domain="clr-namespace:MaterialDesignDemo.Domain"
99
mc:Ignorable="d"
10-
d:DesignHeight="300" d:DesignWidth="600">
10+
d:DesignHeight="300" d:DesignWidth="600" d:DataContext="{d:DesignInstance domain:ListsAndGridsViewModel}">
1111
<UserControl.Resources>
1212
<ResourceDictionary>
1313
<ResourceDictionary.MergedDictionaries>
@@ -16,11 +16,11 @@
1616
</ResourceDictionary>
1717
</UserControl.Resources>
1818
<ScrollViewer>
19-
<StackPanel>
20-
<TextBlock>Custom Columns</TextBlock>
19+
<StackPanel Margin="5 0 0 0">
20+
<TextBlock Style="{StaticResource MaterialDesignHeadline5TextBlock}" Text="Custom Columns"/>
2121
<smtx:XamlDisplay Key="grids_1">
22-
<DataGrid Margin="0 8 0 0" ItemsSource="{Binding Items3}" CanUserSortColumns="True" CanUserAddRows="False" AutoGenerateColumns="False"
23-
materialDesign:DataGridAssist.CellPadding="13 8 8 8" materialDesign:DataGridAssist.ColumnHeaderPadding="8" HeadersVisibility="All">
22+
<DataGrid ItemsSource="{Binding Items1}" CanUserSortColumns="True" CanUserAddRows="False" AutoGenerateColumns="False"
23+
HeadersVisibility="All">
2424
<DataGrid.Resources>
2525
<domain:BindingProxy x:Key="DataContextProxy" Data="{Binding}" />
2626
</DataGrid.Resources>
@@ -32,23 +32,38 @@
3232
<!--padding to allow hit test to pass thru for sorting -->
3333
<Border Background="Transparent" Padding="6 0 6 0" HorizontalAlignment="Center">
3434
<CheckBox HorizontalAlignment="Center"
35-
IsChecked="{Binding Data.IsAllItems3Selected, Source={StaticResource DataContextProxy}}" />
35+
IsChecked="{Binding Data.IsAllItems1Selected, Source={StaticResource DataContextProxy}}" />
3636
</Border>
3737
</DataGridCheckBoxColumn.Header>
3838
</DataGridCheckBoxColumn>
3939
<DataGridTextColumn Binding="{Binding Code}"
40-
Header="Code"
41-
EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}" />
40+
Header="Code"
41+
ElementStyle="{StaticResource MaterialDesignDataGridTextColumnStyle}"
42+
EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}" />
4243
<!-- if you want to use the pop up style (MaterialDesignDataGridTextColumnPopupEditingStyle), you must use MaterialDataGridTextColumn -->
43-
<materialDesign:DataGridTextColumn Binding="{Binding Name}"
44-
Header="Name"
45-
EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnPopupEditingStyle}"
46-
/>
44+
<materialDesign:DataGridTextColumn Header="Name"
45+
ElementStyle="{StaticResource MaterialDesignDataGridTextColumnStyle}"
46+
EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnPopupEditingStyle}">
47+
<materialDesign:DataGridTextColumn.Binding>
48+
<Binding Path="Name">
49+
<Binding.ValidationRules>
50+
<domain:NotEmptyValidationRule />
51+
</Binding.ValidationRules>
52+
</Binding>
53+
</materialDesign:DataGridTextColumn.Binding>
54+
</materialDesign:DataGridTextColumn>
4755
<!-- set a max length to get an indicator in the editor -->
48-
<materialDesign:DataGridTextColumn Binding="{Binding Description}"
49-
Header="Description"
50-
MaxLength="255"
51-
EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnPopupEditingStyle}" />
56+
<DataGridTextColumn Header="Description"
57+
ElementStyle="{StaticResource MaterialDesignDataGridTextColumnStyle}"
58+
EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}">
59+
<DataGridTextColumn.Binding>
60+
<Binding Path="Description">
61+
<Binding.ValidationRules>
62+
<domain:NotEmptyValidationRule />
63+
</Binding.ValidationRules>
64+
</Binding>
65+
</DataGridTextColumn.Binding>
66+
</DataGridTextColumn>
5267
<materialDesign:DataGridTextColumn Binding="{Binding Numeric}"
5368
Header="Number with long header"
5469
Width="120"
@@ -66,7 +81,7 @@
6681
</Style>
6782
</DataGridTextColumn.HeaderStyle>
6883
<DataGridTextColumn.ElementStyle>
69-
<Style TargetType="{x:Type TextBlock}">
84+
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource MaterialDesignDataGridTextColumnStyle}">
7085
<Setter Property="HorizontalAlignment" Value="Right" />
7186
</Style>
7287
</DataGridTextColumn.ElementStyle>
@@ -87,15 +102,14 @@
87102
</DataGrid.Columns>
88103
</DataGrid>
89104
</smtx:XamlDisplay>
90-
<TextBlock Margin="0 24 0 0">Auto Generated Columns</TextBlock>
105+
<TextBlock Style="{StaticResource MaterialDesignHeadline5TextBlock}" Text="Auto Generated Columns" Margin="0 24 0 0"/>
91106
<smtx:XamlDisplay Key="grids_2">
92-
<DataGrid Margin="0 8 0 0" ItemsSource="{Binding Items3}" CanUserSortColumns="True" CanUserAddRows="False" />
107+
<DataGrid ItemsSource="{Binding Items2}" CanUserSortColumns="True" CanUserAddRows="False" />
93108
</smtx:XamlDisplay>
94-
<TextBlock Margin="0 24 0 0">Custom Padding</TextBlock>
109+
<TextBlock Style="{StaticResource MaterialDesignHeadline6TextBlock}" Text="Custom Padding" Margin="0 24 0 0"/>
95110
<smtx:XamlDisplay Key="grids_3">
96-
<DataGrid Margin="0 8 0 0" ItemsSource="{Binding Items3}" CanUserSortColumns="True" CanUserAddRows="False"
97-
materialDesign:DataGridAssist.CellPadding="4 2 2 2" materialDesign:DataGridAssist.ColumnHeaderPadding="4 2 2 2"
98-
/>
111+
<DataGrid ItemsSource="{Binding Items3}" CanUserSortColumns="True" CanUserAddRows="False"
112+
materialDesign:DataGridAssist.CellPadding="4 2 2 2" materialDesign:DataGridAssist.ColumnHeaderPadding="4 2 2 2" />
99113
</smtx:XamlDisplay>
100114
</StackPanel>
101115
</ScrollViewer>

MainDemo.Wpf/Grids.xaml.cs renamed to MainDemo.Wpf/DataGrids.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ namespace MaterialDesignColors.WpfExample
55
/// <summary>
66
/// Interaction logic for Grids.xaml
77
/// </summary>
8-
public partial class Grids : UserControl
8+
public partial class DataGrids : UserControl
99
{
10-
public Grids()
10+
public DataGrids()
1111
{
1212
InitializeComponent();
1313
}

MainDemo.Wpf/Domain/ListsAndGridsViewModel.cs

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,41 @@
11
using System.Collections.Generic;
22
using System.Collections.ObjectModel;
33
using System.ComponentModel;
4-
using System.Runtime.CompilerServices;
5-
using MaterialDesignColors.WpfExample.Domain;
64
using System.Linq;
5+
using System.Runtime.CompilerServices;
76

87
namespace MaterialDesignDemo.Domain
98
{
109
public class ListsAndGridsViewModel : INotifyPropertyChanged
1110
{
12-
private readonly ObservableCollection<SelectableViewModel> _items1;
13-
private readonly ObservableCollection<SelectableViewModel> _items2;
14-
private readonly ObservableCollection<SelectableViewModel> _items3;
15-
1611
public ListsAndGridsViewModel()
1712
{
18-
_items1 = CreateData();
19-
_items2 = CreateData();
20-
_items3 = CreateData();
13+
Items1 = CreateData();
14+
Items2 = CreateData();
15+
Items3 = CreateData();
2116

22-
foreach (var model in _items3)
17+
foreach (var model in Items1)
2318
{
2419
model.PropertyChanged += (sender, args) =>
2520
{
2621
if (args.PropertyName == nameof(SelectableViewModel.IsSelected))
27-
OnPropertyChanged(nameof(IsAllItems3Selected));
22+
OnPropertyChanged(nameof(IsAllItems1Selected));
2823
};
2924
}
3025
}
3126

32-
public bool? IsAllItems3Selected
27+
public bool? IsAllItems1Selected
3328
{
3429
get
3530
{
36-
var selected = _items3.Select(item => item.IsSelected).Distinct().ToList();
31+
var selected = Items1.Select(item => item.IsSelected).Distinct().ToList();
3732
return selected.Count == 1 ? selected.Single() : (bool?) null;
3833
}
3934
set
4035
{
4136
if (value.HasValue)
4237
{
43-
SelectAll(value.Value, Items3);
38+
SelectAll(value.Value, Items1);
4439
OnPropertyChanged();
4540
}
4641
}
@@ -80,10 +75,9 @@ private static ObservableCollection<SelectableViewModel> CreateData()
8075
};
8176
}
8277

83-
public ObservableCollection<SelectableViewModel> Items1 => _items1;
84-
public ObservableCollection<SelectableViewModel> Items2 => _items2;
85-
86-
public ObservableCollection<SelectableViewModel> Items3 => _items3;
78+
public ObservableCollection<SelectableViewModel> Items1 { get; }
79+
public ObservableCollection<SelectableViewModel> Items2 { get; }
80+
public ObservableCollection<SelectableViewModel> Items3 { get; }
8781

8882
public event PropertyChangedEventHandler PropertyChanged;
8983

@@ -92,15 +86,6 @@ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName
9286
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
9387
}
9488

95-
public IEnumerable<string> Foods
96-
{
97-
get
98-
{
99-
yield return "Burger";
100-
yield return "Fries";
101-
yield return "Shake";
102-
yield return "Lettuce";
103-
}
104-
}
89+
public IEnumerable<string> Foods => new[] { "Burger", "Fries", "Shake", "Lettuce" };
10590
}
10691
}

MainDemo.Wpf/Domain/MainWindowViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ private ObservableCollection<DemoItem> GenerateDemoItems(ISnackbarMessageQueue s
207207
DocumentationLink.DemoPageLink<TreesViewModel>("Demo View Model"),
208208
DocumentationLink.StyleLink("TreeView")
209209
}),
210-
new DemoItem("Grids", new Grids { DataContext = new ListsAndGridsViewModel()},
210+
new DemoItem("Data Grids", new DataGrids { DataContext = new ListsAndGridsViewModel()},
211211
new []
212212
{
213-
DocumentationLink.DemoPageLink<Grids>("Demo View"),
213+
DocumentationLink.DemoPageLink<DataGrids>("Demo View"),
214214
DocumentationLink.DemoPageLink<ListsAndGridsViewModel>("Demo View Model", "Domain"),
215215
DocumentationLink.StyleLink("DataGrid")
216216
}),

MainDemo.Wpf/Domain/SelectableViewModel.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
using System.ComponentModel;
1+
using System;
2+
using System.ComponentModel;
23
using System.Runtime.CompilerServices;
34

4-
5-
namespace MaterialDesignColors.WpfExample.Domain
5+
namespace MaterialDesignDemo.Domain
66
{
7-
87
public class SelectableViewModel : INotifyPropertyChanged
98
{
109
private bool _isSelected;
@@ -16,7 +15,7 @@ public class SelectableViewModel : INotifyPropertyChanged
1615

1716
public bool IsSelected
1817
{
19-
get { return _isSelected; }
18+
get => _isSelected;
2019
set
2120
{
2221
if (_isSelected == value) return;
@@ -27,7 +26,7 @@ public bool IsSelected
2726

2827
public char Code
2928
{
30-
get { return _code; }
29+
get => _code;
3130
set
3231
{
3332
if (_code == value) return;
@@ -38,7 +37,7 @@ public char Code
3837

3938
public string Name
4039
{
41-
get { return _name; }
40+
get => _name;
4241
set
4342
{
4443
if (_name == value) return;
@@ -49,7 +48,7 @@ public string Name
4948

5049
public string Description
5150
{
52-
get { return _description; }
51+
get => _description;
5352
set
5453
{
5554
if (_description == value) return;
@@ -60,18 +59,18 @@ public string Description
6059

6160
public double Numeric
6261
{
63-
get { return _numeric; }
62+
get => _numeric;
6463
set
6564
{
66-
if (_numeric == value) return;
65+
if (Math.Abs(_numeric - value) < 0.01) return;
6766
_numeric = value;
6867
OnPropertyChanged();
6968
}
7069
}
7170

7271
public string Food
7372
{
74-
get { return _food; }
73+
get => _food;
7574
set
7675
{
7776
if (_food == value) return;
@@ -84,8 +83,7 @@ public string Food
8483

8584
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
8685
{
87-
var handler = PropertyChanged;
88-
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
86+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
8987
}
9088
}
91-
}
89+
}

MainDemo.Wpf/Lists.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6-
xmlns:domain="clr-namespace:MaterialDesignColors.WpfExample.Domain"
6+
xmlns:domain="clr-namespace:MaterialDesignDemo.Domain"
77
xmlns:smtx="clr-namespace:ShowMeTheXAML;assembly=ShowMeTheXAML"
88
mc:Ignorable="d"
9-
d:DesignHeight="300" d:DesignWidth="300">
9+
d:DesignHeight="300" d:DesignWidth="300" d:DataContext="{d:DesignInstance domain:ListsAndGridsViewModel}">
1010
<UserControl.Resources>
1111
<ResourceDictionary>
1212
<ResourceDictionary.MergedDictionaries>

MaterialDesignThemes.Wpf/DataGridAssist.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,33 @@ public static Style GetAutoGeneratedEditingCheckBoxStyle(DependencyObject elemen
6666
{
6767
return (Style)element.GetValue(AutoGeneratedEditingCheckBoxStyleProperty);
6868
}
69+
70+
public static readonly DependencyProperty AutoGeneratedTextStyleProperty = DependencyProperty
71+
.RegisterAttached(
72+
"AutoGeneratedTextStyle", typeof(Style), typeof(DataGridAssist),
73+
new PropertyMetadata(default(Style), AutoGeneratedTextStylePropertyChangedCallback));
74+
75+
private static void AutoGeneratedTextStylePropertyChangedCallback(DependencyObject dependencyObject,
76+
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
77+
{
78+
((DataGrid)dependencyObject).AutoGeneratingColumn += (sender, args) =>
79+
{
80+
var dataGridTextColumn = args.Column as System.Windows.Controls.DataGridTextColumn;
81+
if (dataGridTextColumn == null) return;
82+
83+
dataGridTextColumn.ElementStyle = GetAutoGeneratedTextStyle(dependencyObject);
84+
};
85+
}
86+
87+
public static void SetAutoGeneratedTextStyle(DependencyObject element, Style value)
88+
{
89+
element.SetValue(AutoGeneratedTextStyleProperty, value);
90+
}
91+
92+
public static Style GetAutoGeneratedTextStyle(DependencyObject element)
93+
{
94+
return (Style)element.GetValue(AutoGeneratedTextStyleProperty);
95+
}
6996

7097
public static readonly DependencyProperty AutoGeneratedEditingTextStyleProperty = DependencyProperty
7198
.RegisterAttached(
@@ -77,7 +104,7 @@ private static void AutoGeneratedEditingTextStylePropertyChangedCallback(Depende
77104
{
78105
((DataGrid)dependencyObject).AutoGeneratingColumn += (sender, args) =>
79106
{
80-
var dataGridTextColumn = args.Column as DataGridTextColumn;
107+
var dataGridTextColumn = args.Column as System.Windows.Controls.DataGridTextColumn;
81108
if (dataGridTextColumn == null) return;
82109

83110
dataGridTextColumn.EditingElementStyle = GetAutoGeneratedEditingTextStyle(dependencyObject);

0 commit comments

Comments
 (0)