Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // View model as per OP's example
- public class ViewModel : INotifyPropertyChanged
- {
- Car _myCarProperty;
- public Car MyCarProperty
- {
- get { return _myCarProperty; }
- set
- {
- if (value == _myCarProperty) return;
- _myCarProperty = value;
- OnPropertyChanged();
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- [NotifyPropertyChangedInvocator]
- protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- // Further viewmodels to demonstrate nested property path binding
- public class Car
- {
- public Car(string make)
- {
- Make = make;
- Model = new Model(make);
- }
- public string Make { get; set; }
- public Model Model { get; }
- }
- public class Model
- {
- public Model(string make)
- {
- Name = make + "tron";
- SuperNested = new SuperNested(Name);
- }
- public string Name { get; }
- public SuperNested SuperNested { get; }
- }
- public class SuperNested
- {
- readonly int _nestLevel;
- readonly string _name;
- public SuperNested(string name, int nestLevel = 0)
- {
- _name = name;
- _nestLevel = nestLevel;
- }
- public SuperNested this[string any] => new SuperNested(_name, _nestLevel + 1);
- public string Name => _name + " L" + _nestLevel;
- }
- // MainWindow.xaml.cs
- public partial class MainWindow
- {
- readonly ViewModel _viewModel;
- public MainWindow()
- {
- _viewModel = new ViewModel {MyCarProperty = new Car("Audi")};
- DataContext = _viewModel;
- InitializeComponent();
- }
- void OnChangeMyCarPropertyClicked(object sender, RoutedEventArgs e)
- {
- // The view will be notified of this property change, no matter how deep the "Path" is.
- _viewModel.MyCarProperty = new Car(_textBox.Text);
- }
- void OnChangeMakeDirectlyClicked(object sender, RoutedEventArgs e)
- {
- //This is the part WPF will not pick up automatically, UNLESS you add NotifyPropertyChanged for the "Make" property.
- _viewModel.MyCarProperty.Make = _textBox.Text;
- }
- }
- // MainWindow.xaml
- <Window
- x:Class="NestedBindingSOExample.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- Title="MainWindow"
- Height="350"
- Width="525">
- <Grid>
- <StackPanel
- VerticalAlignment="Center">
- <StackPanel
- Orientation="Horizontal"
- HorizontalAlignment="Center">
- <TextBlock
- Text="MyCarProperty.Make Xaml Bound:"
- Margin="0,0,10,0" />
- <TextBlock
- Text="{Binding Path=MyCarProperty.Make}" />
- </StackPanel>
- <StackPanel
- Orientation="Horizontal"
- HorizontalAlignment="Center">
- <TextBlock
- Text="MyCarProperty.Model.SuperNested Xaml Bound:"
- Margin="0,0,10,0" />
- <TextBlock
- Text="{Binding Path=MyCarProperty.Model.SuperNested[any][thing][you][want][to][try][and][access].Name}" />
- </StackPanel>
- <Label
- Margin="0,15,0,0"
- HorizontalAlignment="Center"
- Content="Enter New Make:" />
- <TextBox
- Margin="0,3,0,0"
- HorizontalAlignment="Center"
- Width="150"
- x:Name="_textBox" />
- <Button
- Margin="0,7,0,0"
- HorizontalAlignment="Center"
- Width="150"
- Content="Change MyCarProperty"
- Click="OnChangeMyCarPropertyClicked" />
- <Button
- Margin="0,7,0,0"
- HorizontalAlignment="Center"
- Width="150"
- Content="Change Make Name"
- Click="OnChangeMakeDirectlyClicked"
- ToolTip="This wont work out of the box, unless you add NotifyPropertyChanged" />
- </StackPanel>
- </Grid>
- </Window>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement