数据绑定到嵌套属性?
Data Binding to Nested Properties?
我对 WPF 和 XAML 还很陌生,现在我被数据绑定困扰了好几天!我只是想将一些嵌套属性绑定到 TextBox 和 ListView(通过 XAML),但我做错了。
这是我的示例代码:
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>
我的问题:
如何绑定嵌套属性?可能吗?为什么像
Text="{Binding MyModel.MyCounter.CurrentNumber}"
不工作?
XAML中的"DataContext"设置是否正确?
像这样在构造函数中设置数据上下文:
public MainWindow()
{
InitializeComponent();
MyModel = new MyModel { MyCounter = new Counter() };
this.DataContext = MyModel;
}
当然,您的数据路径也会发生变化,因为您要绑定的数据位于 MyModel 下。您的绑定应更改如下:
<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>
编辑:
这就是使用 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>
代码更改如下:
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();
}
}
}
顺便说一下,您应该在 MyModel 构造函数中初始化 _myCounter
。
我不认为你的结论是完全正确的,你实际上忽略了这里的真正问题。
只需将您的属性创建为
private MyModel myModel;
public MyModel MyModel
{
get { return myModel;}
set { myModel = value;
NotifyPropertyChanged("MyModel");
}
}
问题是 MyModel 属性 是在控件初始化之后初始化的,但是绑定引擎如何知道它必须为此刷新视图这个 属性 仍然是空的,因为你还没有告诉引擎拉绑定值。
或者只是在视图 之前初始化 属性 的值。一世。 e.
public MainWindow()
{
MyModel = new MyModel { MyCounter = new Counter() };
InitializeComponent();
}
因此 WPF 确实支持 nested/dotted 绑定。
我对 WPF 和 XAML 还很陌生,现在我被数据绑定困扰了好几天!我只是想将一些嵌套属性绑定到 TextBox 和 ListView(通过 XAML),但我做错了。 这是我的示例代码:
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>
我的问题:
如何绑定嵌套属性?可能吗?为什么像
Text="{Binding MyModel.MyCounter.CurrentNumber}"
不工作?
XAML中的"DataContext"设置是否正确?
像这样在构造函数中设置数据上下文:
public MainWindow()
{
InitializeComponent();
MyModel = new MyModel { MyCounter = new Counter() };
this.DataContext = MyModel;
}
当然,您的数据路径也会发生变化,因为您要绑定的数据位于 MyModel 下。您的绑定应更改如下:
<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>
编辑:
这就是使用 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>
代码更改如下:
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();
}
}
}
顺便说一下,您应该在 MyModel 构造函数中初始化 _myCounter
。
我不认为你的结论是完全正确的,你实际上忽略了这里的真正问题。
只需将您的属性创建为
private MyModel myModel;
public MyModel MyModel
{
get { return myModel;}
set { myModel = value;
NotifyPropertyChanged("MyModel");
}
}
问题是 MyModel 属性 是在控件初始化之后初始化的,但是绑定引擎如何知道它必须为此刷新视图这个 属性 仍然是空的,因为你还没有告诉引擎拉绑定值。
或者只是在视图 之前初始化 属性 的值。一世。 e.
public MainWindow()
{
MyModel = new MyModel { MyCounter = new Counter() };
InitializeComponent();
}
因此 WPF 确实支持 nested/dotted 绑定。