我想成功TwoWay将一个ObservableCollection绑定到DataTemplate中的TextBoxes。我可以正确显示数据,但我无法通过用户界面更改列表数据。我有一个名为'model'的Model类,其中包含一个名为'List'的ObservableCollection。该类实现INotifyPropertyChanged接口。这是shell的xaml。在DataContext的窗口1的网格设置为“theGrid.DataContext =模型”如何将ObservableCollection绑定到DataTemplate中的TextBoxes?
<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>
这是模型类的代码:
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?");
}
}
谁能告知,如果我去约这是正确的方法?
对我的作品!非常感谢!! – Johnathan1 2009-06-30 21:31:12
我不认为你需要在Text属性中放置“Path =”,也可以使用Text =“{Binding Value}” – user1069816 2014-12-23 17:09:37