Skip to content

Commit 6a415e2

Browse files
committed
improve combos in grids...not quite there yet, but closer...
1 parent ac69333 commit 6a415e2

File tree

8 files changed

+467
-6
lines changed

8 files changed

+467
-6
lines changed

MaterialDesignColors.WpfExample/Domain/ListFieldsViewModel.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ private static ObservableCollection<SelectableViewModel> CreateData()
5858
{
5959
Code = 'D',
6060
Name = "Dragablz",
61-
Description = "Dragablz Tab Control"
61+
Description = "Dragablz Tab Control",
62+
Food = "Fries"
6263
},
6364
new SelectableViewModel
6465
{
@@ -92,5 +93,16 @@ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName
9293
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
9394
//PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
9495
}
96+
97+
public IEnumerable<string> Foods
98+
{
99+
get
100+
{
101+
yield return "Burger";
102+
yield return "Fries";
103+
yield return "Shake";
104+
yield return "Lettuce";
105+
}
106+
}
95107
}
96108
}

MaterialDesignColors.WpfExample/Domain/SelectableViewModel.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
namespace MaterialDesignColors.WpfExample.Domain
66
{
7+
78
public class SelectableViewModel : INotifyPropertyChanged
89
{
910
private bool _isSelected;
1011
private string _name;
1112
private string _description;
1213
private char _code;
13-
private double _numeric;
14+
private double _numeric;
15+
private string _food;
1416

1517
public bool IsSelected
1618
{
@@ -65,7 +67,18 @@ public double Numeric
6567
_numeric = value;
6668
OnPropertyChanged();
6769
}
68-
}
70+
}
71+
72+
public string Food
73+
{
74+
get { return _food; }
75+
set
76+
{
77+
if (_food == value) return;
78+
_food = value;
79+
OnPropertyChanged();
80+
}
81+
}
6982

7083
public event PropertyChangedEventHandler PropertyChanged;
7184

MaterialDesignColors.WpfExample/ListsWindow.xaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@
131131
</Style>
132132
</DataGridTextColumn.ElementStyle>
133133
</wpf:MaterialDataGridTextColumn>
134+
135+
<!-- use custom combo box column to get better combos. Use ItemsSourceBinding as your binding template to be applied to each combo -->
136+
<wpf:MaterialDataGridComboBoxColumn Header="Food"
137+
SelectedValueBinding="{Binding Food}"
138+
ItemsSourceBinding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.Foods}" />
134139
</DataGrid.Columns>
135140
</DataGrid>
136141
</TabItem>

MaterialDesignThemes.Wpf/DataGridAssist.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ public static void SetAutoGeneratedEditingTextStyle(DependencyObject element, St
7878
public static Style GetAutoGeneratedEditingTextStyle(DependencyObject element)
7979
{
8080
return (Style) element.GetValue(AutoGeneratedEditingTextStyleProperty);
81-
}
81+
}
8282
}
8383
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System.Windows;
2+
using System.Windows.Controls;
3+
using System.Windows.Data;
4+
5+
namespace MaterialDesignThemes.Wpf
6+
{
7+
public class MaterialDataGridComboBoxColumn : DataGridComboBoxColumn //DataGridBoundColumn
8+
{
9+
static MaterialDataGridComboBoxColumn()
10+
{
11+
ElementStyleProperty.OverrideMetadata(typeof(MaterialDataGridComboBoxColumn), new FrameworkPropertyMetadata(DefaultElementStyle));
12+
EditingElementStyleProperty.OverrideMetadata(typeof(MaterialDataGridComboBoxColumn), new FrameworkPropertyMetadata(DefaultEditingElementStyle));
13+
}
14+
15+
public Binding ItemsSourceBinding { get; set; }
16+
17+
protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
18+
{
19+
var comboBox = base.GenerateElement(cell, cell);
20+
21+
if (ItemsSourceBinding != null)
22+
comboBox.SetBinding(ItemsControl.ItemsSourceProperty, ItemsSourceBinding);
23+
ApplyStyle(false, false, comboBox);
24+
25+
return comboBox;
26+
}
27+
28+
protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
29+
{
30+
var comboBox = (ComboBox)base.GenerateElement(cell, cell);
31+
32+
if (ItemsSourceBinding != null)
33+
comboBox.SetBinding(ItemsControl.ItemsSourceProperty, ItemsSourceBinding);
34+
ApplyStyle(true, false, comboBox);
35+
36+
return comboBox;
37+
}
38+
39+
public static new Style DefaultElementStyle
40+
{
41+
get
42+
{
43+
var comboBox = new ComboBox();
44+
45+
var brushKey = new ComponentResourceKey(typeof(ComboBox), "MaterialDataGridComboBoxColumnStyle");
46+
var style = (Style)comboBox.TryFindResource(brushKey);
47+
48+
return style;
49+
}
50+
}
51+
52+
public static new Style DefaultEditingElementStyle
53+
{
54+
get
55+
{
56+
var comboBox = new ComboBox();
57+
58+
var brushKey = new ComponentResourceKey(typeof(ComboBox), "MaterialDataGridComboBoxColumnEditingStyle");
59+
var style = (Style)comboBox.TryFindResource(brushKey);
60+
61+
return style;
62+
}
63+
}
64+
65+
private void ApplyStyle(bool isEditing, bool defaultToElementStyle, FrameworkElement element)
66+
{
67+
var style = PickStyle(isEditing, defaultToElementStyle);
68+
if (style != null)
69+
{
70+
element.Style = style;
71+
}
72+
}
73+
74+
private Style PickStyle(bool isEditing, bool defaultToElementStyle)
75+
{
76+
var style = isEditing ? EditingElementStyle : ElementStyle;
77+
if (isEditing && defaultToElementStyle && (style == null))
78+
{
79+
style = ElementStyle;
80+
}
81+
82+
return style;
83+
}
84+
85+
/*
86+
/// <summary>
87+
/// Assigns the Binding to the desired property on the target object.
88+
/// </summary>
89+
private void ApplyBinding(DependencyObject target, DependencyProperty property)
90+
{
91+
var binding = Binding;
92+
if (binding != null)
93+
{
94+
BindingOperations.SetBinding(target, property, binding);
95+
}
96+
else
97+
{
98+
BindingOperations.ClearBinding(target, property);
99+
}
100+
}
101+
*/
102+
103+
}
104+
}

MaterialDesignThemes.Wpf/MaterialDesignThemes.Wpf.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@
8080
<SubType>Designer</SubType>
8181
<Generator>MSBuild:Compile</Generator>
8282
</Page>
83+
<Page Include="Themes\MaterialDesignTheme.DataGrid.ComboBox.xaml">
84+
<SubType>Designer</SubType>
85+
<Generator>MSBuild:Compile</Generator>
86+
</Page>
8387
<Page Include="Themes\MaterialDesignTheme.DataGrid.xaml">
8488
<SubType>Designer</SubType>
8589
<Generator>MSBuild:Compile</Generator>
@@ -165,6 +169,7 @@
165169
<Compile Include="DataGridAssist.cs" />
166170
<Compile Include="DateTimeEx.cs" />
167171
<Compile Include="ListSortDirectionIndicator.cs" />
172+
<Compile Include="MaterialDataGridComboBoxColumn.cs" />
168173
<Compile Include="MaterialDataGridTextColumn.cs" />
169174
<Compile Include="MaterialDateDisplay.cs" />
170175
<Compile Include="PaletteHelper.cs" />

0 commit comments

Comments
 (0)