我对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();
}
}
}
顺便说一下,您应该_myCounter
在MyModel构造函数中进行初始化。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句