Advertisement
sbot1101

WPF Binding nested path demo

May 5th, 2018
1,778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.44 KB | None | 0 0
  1. // View model as per OP's example
  2. public class ViewModel : INotifyPropertyChanged
  3. {
  4.     Car _myCarProperty;
  5.  
  6.     public Car MyCarProperty
  7.     {
  8.         get { return _myCarProperty; }
  9.         set
  10.         {
  11.             if (value == _myCarProperty) return;
  12.  
  13.             _myCarProperty = value;
  14.             OnPropertyChanged();
  15.         }
  16.     }
  17.  
  18.     public event PropertyChangedEventHandler PropertyChanged;
  19.  
  20.     [NotifyPropertyChangedInvocator]
  21.     protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  22.     {
  23.         PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  24.     }
  25. }
  26.  
  27. // Further viewmodels to demonstrate nested property path binding
  28. public class Car
  29. {
  30.     public Car(string make)
  31.     {
  32.         Make = make;
  33.         Model = new Model(make);
  34.     }
  35.  
  36.     public string Make { get; set; }
  37.  
  38.     public Model Model { get; }
  39. }
  40.  
  41. public class Model
  42. {
  43.     public Model(string make)
  44.     {
  45.         Name = make + "tron";
  46.         SuperNested = new SuperNested(Name);
  47.     }
  48.  
  49.     public string Name { get; }
  50.  
  51.     public SuperNested SuperNested { get; }
  52. }
  53.  
  54. public class SuperNested
  55. {
  56.     readonly int _nestLevel;
  57.     readonly string _name;
  58.  
  59.     public SuperNested(string name, int nestLevel = 0)
  60.     {
  61.         _name = name;
  62.         _nestLevel = nestLevel;
  63.     }
  64.  
  65.     public SuperNested this[string any] => new SuperNested(_name, _nestLevel + 1);
  66.  
  67.     public string Name => _name + " L" + _nestLevel;
  68. }
  69.  
  70.  
  71. // MainWindow.xaml.cs
  72. public partial class MainWindow
  73. {
  74.     readonly ViewModel _viewModel;
  75.  
  76.     public MainWindow()
  77.     {
  78.         _viewModel = new ViewModel {MyCarProperty = new Car("Audi")};
  79.  
  80.         DataContext = _viewModel;
  81.  
  82.         InitializeComponent();
  83.     }
  84.  
  85.     void OnChangeMyCarPropertyClicked(object sender, RoutedEventArgs e)
  86.     {
  87.         // The view will be notified of this property change, no matter how deep the "Path" is.
  88.         _viewModel.MyCarProperty = new Car(_textBox.Text);
  89.     }
  90.  
  91.     void OnChangeMakeDirectlyClicked(object sender, RoutedEventArgs e)
  92.     {
  93.         //This is the part WPF will not pick up automatically, UNLESS you add NotifyPropertyChanged for the "Make" property.
  94.         _viewModel.MyCarProperty.Make = _textBox.Text;
  95.     }
  96. }
  97.  
  98. // MainWindow.xaml
  99. <Window
  100.     x:Class="NestedBindingSOExample.MainWindow"
  101.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  102.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  103.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  104.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  105.     mc:Ignorable="d"
  106.     Title="MainWindow"
  107.     Height="350"
  108.     Width="525">
  109.     <Grid>
  110.         <StackPanel
  111.             VerticalAlignment="Center">
  112.             <StackPanel
  113.                 Orientation="Horizontal"
  114.                 HorizontalAlignment="Center">
  115.                 <TextBlock
  116.                     Text="MyCarProperty.Make Xaml Bound:"
  117.                     Margin="0,0,10,0" />
  118.                 <TextBlock
  119.                     Text="{Binding Path=MyCarProperty.Make}" />
  120.             </StackPanel>
  121.             <StackPanel
  122.                 Orientation="Horizontal"
  123.                 HorizontalAlignment="Center">
  124.                 <TextBlock
  125.                     Text="MyCarProperty.Model.SuperNested Xaml Bound:"
  126.                     Margin="0,0,10,0" />
  127.                 <TextBlock
  128.                     Text="{Binding Path=MyCarProperty.Model.SuperNested[any][thing][you][want][to][try][and][access].Name}" />
  129.             </StackPanel>
  130.             <Label
  131.                 Margin="0,15,0,0"
  132.                 HorizontalAlignment="Center"
  133.                 Content="Enter New Make:" />
  134.             <TextBox
  135.                 Margin="0,3,0,0"
  136.                 HorizontalAlignment="Center"
  137.                 Width="150"
  138.                 x:Name="_textBox" />
  139.             <Button
  140.                 Margin="0,7,0,0"
  141.                 HorizontalAlignment="Center"
  142.                 Width="150"
  143.                 Content="Change MyCarProperty"
  144.                 Click="OnChangeMyCarPropertyClicked" />
  145.             <Button
  146.                 Margin="0,7,0,0"
  147.                 HorizontalAlignment="Center"
  148.                 Width="150"
  149.                 Content="Change Make Name"
  150.                 Click="OnChangeMakeDirectlyClicked"
  151.                 ToolTip="This wont work out of the box, unless you add NotifyPropertyChanged" />
  152.         </StackPanel>
  153.     </Grid>
  154. </Window>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement