如何将 ObservableCollection 绑定到 DataTemplate 中的文本框?
我正在尝试成功地将 ObservableCollection 绑定到 DataTemplate 中的 TextBoxes。 我可以让数据正确显示,但无法通过 UI 更改列表数据。 我有一个名为“model”的模型类,其中包含一个名为“List”的 ObservableCollection。 该类实现 INotifyPropertyChanged 接口。 这是 shell 的 xaml。 Window1 网格的 DataContext 设置为“theGrid.DataContext=model”
<Window x:Class="BindThat.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BindThat"
Title="Window1" Height="300" Width="300">
<StackPanel x:Name="theGrid">
<GroupBox BorderBrush="LightGreen">
<GroupBox.Header>
<TextBlock Text="Group" />
</GroupBox.Header>
<ItemsControl ItemsSource="{Binding Path=List}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</GroupBox>
</StackPanel>
这是 Model 类的代码:
class Model : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
private ObservableCollection<string> _list = new ObservableCollection<string>();
public ObservableCollection<string> List
{
get { return _list; }
set
{
_list = value;
NotifyPropertyChanged("List");
}
}
public Model()
{
List.Add("why");
List.Add("not");
List.Add("these?");
}
}
任何人都可以建议我是否以正确的方式处理这个问题?
I am trying to successfully TwoWay bind an ObservableCollection to TextBoxes in a DataTemplate. I can get the data to display properly, but I am unable to change the list data through the UI. I have a Model class named 'model' which contains an ObservableCollection named 'List'. The class implements the INotifyPropertyChanged interface. Here is the xaml for the shell. The DataContext for Window1's grid is set to "theGrid.DataContext=model"
<Window x:Class="BindThat.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BindThat"
Title="Window1" Height="300" Width="300">
<StackPanel x:Name="theGrid">
<GroupBox BorderBrush="LightGreen">
<GroupBox.Header>
<TextBlock Text="Group" />
</GroupBox.Header>
<ItemsControl ItemsSource="{Binding Path=List}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</GroupBox>
</StackPanel>
This is the code for the Model class:
class Model : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
private ObservableCollection<string> _list = new ObservableCollection<string>();
public ObservableCollection<string> List
{
get { return _list; }
set
{
_list = value;
NotifyPropertyChanged("List");
}
}
public Model()
{
List.Add("why");
List.Add("not");
List.Add("these?");
}
}
Could anyone advise if I am going about this the correct way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要一个属性来绑定两种方式,因此字符串不适合这种情况。
将其包装在字符串对象中,如下所示:
并绑定到 Value 属性而不是“。”
另外,您不需要通知可观察集合中的更改,因此在您的模型拥有自己的其他属性之前,它不需要具有 INotifyPropertyChange。 如果您希望 ItemsControl 对各个 StringObject 中的更改做出反应,则应将 INotifyPropertyChanged 添加到 StringObject。
再说一次,双向绑定是默认的,因此您只需要
在绑定中。
You need a property to bind two way, so string is not good for this.
Wrap it in a string object, like this:
and bind to Value property instead of "."
Also, you don't need to notify of a change in observable collection, so until your model has some other propertis of its own, it does not need to have INotifyPropertyChange. If you want your ItemsControl react to changes in the individual StringObjects, then you should add INotifyPropertyChanged to a StringObject.
And yet again, two way binding is default, so you need only
in your binding.
我相信您需要从 DependencyObject 派生集合项才能使 TwoWay 绑定发挥作用。 就像是:
...
I believe you need to derive your collection items from DependencyObject for TwoWay binding to work. Something like:
...
xaml 视图:
在构造函数后面的代码中:
在 ViewModel 类中:
要小心
string
类型的集合,它不起作用,您必须使用对象 =>字符串对象
xaml view:
in code behind in the constructor:
in ViewModel Class:
Be careful with a collection with type
string
it doesn't work, you have to use an object =>StringObject