Estoy tratando de enlazar TwoWay con éxito un ObservableCollection a TextBoxes en un DataTemplate. Puedo hacer que los datos se muestren correctamente, pero no puedo cambiar los datos de la lista a través de la IU. Tengo una clase de modelo llamada 'modelo' que contiene una ObservableCollection llamada 'Lista'. La clase implementa la interfaz INotifyPropertyChanged. Aquí está el xaml para el caparazón. El DataContext para la red de Window1 se establece en "theGrid.DataContext = modelo"¿Cómo puedo vincular un ObservableCollection a TextBoxes en 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>
Este es el código de la clase del modelo:
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?");
}
}
Podría cualquier persona aconsejar si voy acerca esta es la forma correcta?
funciona para mí! ¡¡Muchas gracias!! – Johnathan1
No creo que deba poner "Path =" en la propiedad Text, 'Text =" {Binding Value} "' también funcionaría – user1069816
¿Por qué funciona la cadena simple pero no la lista? –
YukiSakura