Проблемы сбором данных MVVM
Есть такая коллекция Description:
public ObservableCollection<string> Description { get; set; } = new ObservableCollection<string>();
Есть такой простой шаблон данных одним котролом TextBox :
<!--Описание товара-->
<StackPanel Grid.Row="6" >
<ItemsControl ItemsSource="{Binding Description}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="39*"/>
</Grid.RowDefinitions>
<TextBox Style="{StaticResource BaseTextBox}" VerticalAlignment="Center" Margin="1" Tag="Введите описание товара" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button
Margin="4"
Command="{Binding AddDescription}"
Content="Добавить описание товара"
Style="{StaticResource ButtonAdd}" />
</StackPanel>
Каманда AddDescription
public ICommand AddDescription
{
get
{
return new DelegateCommand((obj) =>
{
Description.Add(" ");
});
}
}
Проблема:Команда AddDescription простая простая добавляет пустые TextBox,когда пользователь вводит данные они не сохраняться в коллекции Description. Я не знаю как правильно привязаться к данным чтобы они изменяются(сохраняется). Задумка такая я добавляю 4 пустых TextBox пользователь вводит данные если ему не достаточно контролов 4 он нажимать на кнопку и добавляет еще 1.
Ответы (1 шт):
Автор решения: Vladimir
→ Ссылка
DescriptionModel
class DescriptionModel:ChangeProperty
{
string text { get; set; }
public string Text
{
get { return text; }
set
{
text = value;
OnPropertyChanged("Text");
}
}
public DescriptionModel() { }
public DescriptionModel(string text)
{
Text = text;
}
}
Descriptions
public ObservableCollection<DescriptionModel> Descriptions { get; set; } = new ObservableCollection<DescriptionModel>();
Привязка данных
<StackPanel Grid.Row="6" >
<ItemsControl ItemsSource="{Binding offer.Descriptions}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="39*"/>
</Grid.RowDefinitions>
<TextBox Text="{Binding Text,UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource BaseTextBox}" VerticalAlignment="Center" Margin="1" Tag="Введите описание товара" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button
Margin="4"
Command="{Binding AddDescription}"
Content="Добавить описание товара"
Style="{StaticResource ButtonAdd}" />
</StackPanel>