【发布时间】:2015-01-01 13:53:55
【问题描述】:
我对 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”设置是否正确?
【问题讨论】:
-
与论坛网站不同,我们不使用“谢谢”、“任何帮助表示赞赏”或Stack Overflow 上的签名。请参阅“Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?.
-
在初始化 MyModel 属性后对其进行更改。其他一切都是正确的。
标签: c# wpf data-binding