9
I am working on a WPF application and following MVVM. In my view there is a grid view which contains different columns. One of these column is a ListBox. Now problem is that for the ListBox column, SelectedItem get works fine but set doesn't.
Here is my View code
<DataGrid ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" SelectionMode="Single">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="Name" />
<DataGridTemplateColumn Header="Actions">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListBox DisplayMemberPath="Name" ItemsSource="{Binding Actions}" SelectedItem="{Binding SelectedAction}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
In my ViewModel, I have Main ViewModel class, which contains a list of Items. Item class contains name, a list of actions and selected action.
public class MyViewModel : INotifyOfPropertyChanged
{
private ObservableCollection<Item> _items;
public ObservableCollection<Item> Items
{
get { return _items?? (_items= new ObservableCollection<Item>); }
}
private Item _selectedItem;
public Item SelectedItem
{
get { return _selectedItem; }
set { _selectedItem= value; }
}
}
public class Item : INotifyOfPropertyChanged
{
public string Name;
private ObservableCollection<string> _actions;
public ObservableCollection<string> Actions
{
get { return _actions?? (_actions= new ObservableCollection<string>); }
}
private string _selectedAction;
public string SelectedAction
{
get { return _selectedAction; }
set { _selectedAction = value; }
}
}