Sto tentando di associare correttamente TwoWay a ObservableCollection a TextBoxes in un DataTemplate. Posso visualizzare correttamente i dati, ma non riesco a modificare i dati dell'elenco tramite l'interfaccia utente. Ho una classe Model chiamata 'model' che contiene una ObservableCollection chiamata 'List'. La classe implementa l'interfaccia INotifyPropertyChanged. Ecco l'xaml per la shell. Il DataContext per la griglia di Window1 è impostato su "theGrid.DataContext = modello"Come posso associare una ObservableCollection a TextBoxes in un DataTemplate?
<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>
Questo è il codice per la classe del modello:
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?");
}
}
Qualcuno potrebbe avvisare se sto andando su questo è il modo corretto?
funziona per me! Grazie mille!! – Johnathan1
Non penso che sia necessario inserire "Path =" nella proprietà Text, "Text =" {Binding Value} "' funzionerebbe anche – user1069816
Perché funziona a stringa singola ma non nell'elenco? –
YukiSakura