Skip to content

Commit 87e61f1

Browse files
authored
Fixed binding errors in demo app (#1765)
* Fixed binding errors for nullable Kind property * Fixed "Select all"-Binding for first DataGrid using BindingProxy
1 parent 7602669 commit 87e61f1

File tree

5 files changed

+59
-17
lines changed

5 files changed

+59
-17
lines changed

MainDemo.Wpf/Domain/BindingProxy.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Windows;
2+
3+
namespace MaterialDesignDemo.Domain
4+
{
5+
public class BindingProxy : Freezable
6+
{
7+
protected override Freezable CreateInstanceCore() => new BindingProxy();
8+
9+
public object Data
10+
{
11+
get => GetValue(DataProperty);
12+
set => SetValue(DataProperty, value);
13+
}
14+
15+
public static readonly DependencyProperty DataProperty =
16+
DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
17+
}
18+
}

MainDemo.Wpf/Domain/ListsAndGridsViewModel.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.ComponentModel;
44
using System.Runtime.CompilerServices;
55
using MaterialDesignColors.WpfExample.Domain;
6+
using System.Linq;
67

78
namespace MaterialDesignDemo.Domain
89
{
@@ -11,28 +12,37 @@ public class ListsAndGridsViewModel : INotifyPropertyChanged
1112
private readonly ObservableCollection<SelectableViewModel> _items1;
1213
private readonly ObservableCollection<SelectableViewModel> _items2;
1314
private readonly ObservableCollection<SelectableViewModel> _items3;
14-
private bool? _isAllItems3Selected;
1515

1616
public ListsAndGridsViewModel()
1717
{
1818
_items1 = CreateData();
1919
_items2 = CreateData();
2020
_items3 = CreateData();
21+
22+
foreach (var model in _items3)
23+
{
24+
model.PropertyChanged += (sender, args) =>
25+
{
26+
if (args.PropertyName == nameof(SelectableViewModel.IsSelected))
27+
OnPropertyChanged(nameof(IsAllItems3Selected));
28+
};
29+
}
2130
}
2231

2332
public bool? IsAllItems3Selected
2433
{
25-
get { return _isAllItems3Selected; }
34+
get
35+
{
36+
var selected = _items3.Select(item => item.IsSelected).Distinct().ToList();
37+
return selected.Count == 1 ? selected.Single() : (bool?) null;
38+
}
2639
set
2740
{
28-
if (_isAllItems3Selected == value) return;
29-
30-
_isAllItems3Selected = value;
31-
32-
if (_isAllItems3Selected.HasValue)
33-
SelectAll(_isAllItems3Selected.Value, Items3);
34-
35-
OnPropertyChanged();
41+
if (value.HasValue)
42+
{
43+
SelectAll(value.Value, Items3);
44+
OnPropertyChanged();
45+
}
3646
}
3747
}
3848

MainDemo.Wpf/Grids.xaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
77
xmlns:smtx="clr-namespace:ShowMeTheXAML;assembly=ShowMeTheXAML"
8+
xmlns:domain="clr-namespace:MaterialDesignDemo.Domain"
89
mc:Ignorable="d"
910
d:DesignHeight="300" d:DesignWidth="600">
1011
<UserControl.Resources>
@@ -20,16 +21,18 @@
2021
<smtx:XamlDisplay Key="grids_1">
2122
<DataGrid Margin="0 8 0 0" ItemsSource="{Binding Items3}" CanUserSortColumns="True" CanUserAddRows="False" AutoGenerateColumns="False"
2223
materialDesign:DataGridAssist.CellPadding="13 8 8 8" materialDesign:DataGridAssist.ColumnHeaderPadding="8" HeadersVisibility="All">
24+
<DataGrid.Resources>
25+
<domain:BindingProxy x:Key="DataContextProxy" Data="{Binding}" />
26+
</DataGrid.Resources>
2327
<DataGrid.Columns>
24-
<DataGridCheckBoxColumn Binding="{Binding IsSelected}"
28+
<DataGridCheckBoxColumn Binding="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}"
2529
ElementStyle="{StaticResource MaterialDesignDataGridCheckBoxColumnStyle}"
2630
EditingElementStyle="{StaticResource MaterialDesignDataGridCheckBoxColumnEditingStyle}">
2731
<DataGridCheckBoxColumn.Header>
2832
<!--padding to allow hit test to pass thru for sorting -->
2933
<Border Background="Transparent" Padding="6 0 6 0" HorizontalAlignment="Center">
3034
<CheckBox HorizontalAlignment="Center"
31-
DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext}"
32-
IsChecked="{Binding IsAllItems3Selected}" />
35+
IsChecked="{Binding Data.IsAllItems3Selected, Source={StaticResource DataContextProxy}}" />
3336
</Border>
3437
</DataGridCheckBoxColumn.Header>
3538
</DataGridCheckBoxColumn>

MainDemo.Wpf/IconPack.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
GotFocus="TextBox_OnGotFocus"
8585
Text="{Binding Kind, StringFormat='&lt;materialDesign:PackIcon Kind=&quot;{0}&quot; \/>'}" />
8686
</materialDesign:ColorZone>
87-
<materialDesign:PackIcon Kind="{Binding Kind}" VerticalAlignment="Center"
87+
<materialDesign:PackIcon Kind="{Binding PackIconKind}" VerticalAlignment="Center"
8888
Visibility="{Binding Kind, Converter={StaticResource NullableToVisibilityConverter}}" />
8989
<Button Margin="8 0" Command="{Binding CopyToClipboardCommand, Mode=OneTime}" CommandParameter="{Binding Kind}">
9090
<StackPanel Orientation="Horizontal">

MainDemo.Wpf/IconPackViewModel.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public IconPackViewModel(ISnackbarMessageQueue snackbarMessageQueue)
3939
private IEnumerable<PackIconKindGroup> _kinds;
4040
private PackIconKindGroup _group;
4141
private string _kind;
42+
private PackIconKind _packIconKind;
4243

4344
public IEnumerable<PackIconKindGroup> Kinds
4445
{
@@ -55,14 +56,23 @@ public PackIconKindGroup Group
5556
{
5657
Kind = value?.Kind;
5758
}
58-
5959
}
6060
}
6161

6262
public string Kind
6363
{
6464
get => _kind;
65-
set => this.MutateVerbose(ref _kind, value, e => PropertyChanged?.Invoke(this, e));
65+
set
66+
{
67+
if (this.MutateVerbose(ref _kind, value, e => PropertyChanged?.Invoke(this, e)))
68+
PackIconKind = value != null ? (PackIconKind) Enum.Parse(typeof(PackIconKind), value) : default;
69+
}
70+
}
71+
72+
public PackIconKind PackIconKind
73+
{
74+
get => _packIconKind;
75+
set => this.MutateVerbose(ref _packIconKind, value, e => PropertyChanged?.Invoke(this, e));
6676
}
6777

6878
private void OpenDotCom(object obj)
@@ -79,7 +89,8 @@ private async void Search(object obj)
7989
{
8090
Kinds = await Task.Run(() => _packIconKinds.Value
8191
.Where(x => x.Aliases.Any(a => a.IndexOf(text, StringComparison.CurrentCultureIgnoreCase) >= 0))
82-
.ToList());}
92+
.ToList());
93+
}
8394
}
8495

8596
private void CopyToClipboard(object obj)

0 commit comments

Comments
 (0)