[英]Binding cascading combobox itemsource in wpf datagrid using mvvm
How to bind a 2nd dropdown based on first dropdown selected value of first dropdown using mvvm如何使用 mvvm 根据第一个下拉列表的第一个下拉列表选择值绑定第二个下拉列表
Here is the class strcture这是类结构
List<Location> Locations; //Application global cached data
List<Room> Room; //Application global cached data
class Location {LocationId, Name ....}
class Room{RoomId, Name, LocationId...}
XAML XAML
<DataGridTemplateColumn Header="Property Name">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Name="LocationsComboBox"
ItemsSource="{Binding Path=DataContext.Locations, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"
DisplayMemberPath="Name" SelectedValuePath="Id"
SelectedValue="{Binding PropertyId, UpdateSourceTrigger=PropertyChanged}">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<!--Room Number-->
<DataGridTemplateColumn Header="Room Number">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Name="RoomComboBox"
ItemsSource="{Binding Path=DataContext.Rooms, RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"
DisplayMemberPath="RoomName" SelectedValuePath="RoomId"
SelectedValue="{Binding NewRoomId, UpdateSourceTrigger=PropertyChanged}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction
Command="{Binding DataContext.PropertyChangedCommand,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
Did you use INotifypropertychanged ?你用过 INotifypropertychanged 吗? you should implement INotifyPropertyChanged and change your child list when parent was changed您应该实现 INotifyPropertyChanged 并在更改父项时更改您的子项列表
I prefer Master Slave/Details way of combo box.我更喜欢组合框的 Master Slave/Details 方式。 U can find Here你可以在这里找到
The binding for Room ComboBox should be from Code Behind on the basis of selected LocationID. Room ComboBox 的绑定应该来自基于所选 LocationID 的代码隐藏。
the below binding下面的绑定
ItemsSource="{Binding Path=DataContext.Rooms..
should be something like this应该是这样的
ItemsSource="{Binding Path=DataContext.RoomsInSelectedLocation
and in ViewModel并在 ViewModel
IEnumerable<Room> RoomsInSelectedLocation
{
return Rooms.where(r => r.LocationId == SelectedLocationId);
}
evaluate this every time the Location Combo selected item changes.每次“位置组合”所选项目发生更改时,都会对此进行评估。
Use ObservableCollection<Room> instead of List (this will cause the second combobox to update when the first combo box changes the location which in turn causes the room collection to change.使用 ObservableCollection<Room> 而不是 List(这将导致第二个组合框在第一个组合框更改位置时更新,从而导致房间集合发生更改。
Use ObservableCollection<Location> also.也使用 ObservableCollection<Location>。 Your locations might not ever change, but this is simply good MVVM form.您的位置可能永远不会改变,但这只是一个很好的 MVVM 形式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.