我想成功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