Data binding to attached properties?

I am new to WPF and XAML and now I am stuck with data binding for days! I just wanted to bind some attached properties to the TextBox and ListView (via XAML) but I am doing it wrong. Here's my example code:

MainWindow.xaml.cs

namespace CounterTestNestedDataBinding
{
    public partial class MainWindow : Window
    {
        public MyModel MyModel { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            MyModel = new MyModel { MyCounter = new Counter() };
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MyModel.MyCounter.incrementCounter();
        }
    }
}

      

MyModel.cs

namespace CounterTestNestedDataBinding
{
    public class MyModel : INotifyPropertyChanged
    {
        public Counter _myCounter;
        public Counter MyCounter
        {
            get { return _myCounter; }
            set
            {
                _myCounter = value;
                NotifyPropertyChanged("MyCounter");
            }
        }

        // some other members and properties ...

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

    }
}

      

Counter.cs

namespace CounterTestNestedDataBinding
{
    public class Counter : INotifyPropertyChanged
    {
        #region Members
        private int _currentNumber;
        private ObservableCollection<int> _historyList;
        #endregion

        #region Constructor
        public Counter()
        {
            _currentNumber = 0;
            _historyList = new ObservableCollection<int>();
        }
        #endregion

        #region Properties
        public int CurrentNumber
        {
            get { return _currentNumber; }
            set
            {
                _currentNumber = value;
                NotifyPropertyChanged("CurrentNumber");
            }
        }

        public ObservableCollection<int> HistoryList
        {
            get { return _historyList; }
            set
            {
                _historyList = value;
                NotifyPropertyChanged("HistoryList");
            }
        }
        #endregion

        public void incrementCounter()
        {
            HistoryList.Add(CurrentNumber);
            CurrentNumber++;
        }

        public override string ToString()
        {
            return string.Format("CurrentNumber: {0}, HistoryList: {1}", _currentNumber, String.Join(",", _historyList));
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }
}

      

MainWindow.xaml

<Window x:Class="CounterTestNestedDataBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:counterTestNestedDataBinding="clr-namespace:CounterTestNestedDataBinding"
        Title="MainWindow" Height="350" Width="200" ResizeMode="NoResize" WindowStartupLocation="CenterScreen"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        >
    <StackPanel Orientation="Vertical">
        <TextBox x:Name="TextBoxCounterCurrent" Text="{Binding MyModel.MyCounter.CurrentNumber}"/>
        <Button Content="Button" Click="Button_Click"/>
        <ListView x:Name="ListViewCounterHistory" Height="75" ItemsSource="{Binding MyModel.MyCounter.HistoryList}"></ListView>
    </StackPanel>
</Window>

      

My questions:

  • How to bind nested properties? Is it possible? Why is something like

    Text="{Binding MyModel.MyCounter.CurrentNumber}"
    
          

    does not work?

  • Is the "DataContext" set correctly in XAML?

+3


source to share


2 answers


Set your data context in the constructor like this:

public MainWindow()
{
    InitializeComponent();
    MyModel = new MyModel { MyCounter = new Counter() };
    this.DataContext = MyModel;
}

      

and then of course the path to your data will change because the data you are linking is under MyModel. Your bindings should be changed like this:

<StackPanel Orientation="Vertical">
    <TextBox x:Name="TextBoxCounterCurrent" Text="{Binding MyCounter.CurrentNumber}"/>
    <Button Content="Button" Click="Button_Click"/>
    <ListView x:Name="ListViewCounterHistory" Height="75" ItemsSource="{Binding MyCounter.HistoryList}"></ListView>
</StackPanel>

      

EDIT:

This is how you do it using XAML.



<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="525" >
<Window.Resources>
    <local:MyModel x:Key="myModal" />
</Window.Resources>

<StackPanel Orientation="Vertical" DataContext="{StaticResource myModal}">
    <TextBox x:Name="TextBoxCounterCurrent" Text="{Binding MyCounter.CurrentNumber}"/>
    <Button Content="Button" Click="Button_Click"/>
    <ListView x:Name="ListViewCounterHistory" Height="75" ItemsSource="{Binding MyCounter.HistoryList}"></ListView>
</StackPanel>

      

and the code changes like this:

 public partial class MainWindow : Window
{
    public MyModel MyModel { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        //MyModel = new MyModel { MyCounter = new Counter() };
        //this.DataContext = MyModel;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var myModel = this.Resources["myModal"] as MyModel;
        if (myModel != null)
        {
            myModel.MyCounter.incrementCounter();
        }
    }
}

      

By the way, you have to initialize _myCounter

in the constructor of MyModel.

+1


source


I don't think your conclusions are entirely correct and you have actually ignored the real problem here.

just create your property like

private MyModel myModel;

    public MyModel MyModel
    {
        get { return myModel;}
        set { myModel = value;
            NotifyPropertyChanged("MyModel");
        }
    }

      

the problem was that the MyModel property was initialized after the control was initialized, but as the binding engine needs to know that it needs to update the view since this property is still null, t tell the engine to pull out the bound value.



or just initialize the property value before the view, which will work too . I am. e.

public MainWindow()
    {
        MyModel = new MyModel { MyCounter = new Counter() };    
        InitializeComponent();                   
    }

      

Thus WPF supports nested / dashed linking.

0


source







All Articles