在listview的ComboBox,ViewModel类得不到ComboBox的 SelectedModeIndex 和 SelectionChanged事件。
问题描述:
1. 在listview中有ComboBox
2. 数据源类 InspectionInfo ,其中有ComboBox的绑定数据源 ModelList,代码如下:
public class InspectionInfo : BindableBase{/// <summary>/// 模式名称列表 /// </summary>private ObservableCollection<BaseModeOption> _modelList;public ObservableCollection<BaseModeOption> ModelList{get{return _modelList;}set{SetProperty(ref _modelList, value);}}private ExamePartsStat _examePartInfo;public ExamePartsStat ExamePartInfo{get{return _examePartInfo;}set{SetProperty(ref _examePartInfo, value);}}private Inspection _currentInspection;/// <summary>/// 当前的多项检查/// </summary>public Inspection CurrentInspection{get => _currentInspection;set{SetProperty(ref _currentInspection, value);}}private int _selectedModeIndex = 0;public int SelectedModeIndex{get => _selectedModeIndex;set => SetProperty(ref _selectedModeIndex, value);}private DelegateCommand<object> _selectionModeChangedCommand;/// <summary>/// 模式改变事件/// </summary>public DelegateCommand<object> SelectionModeChangedCommand =>_selectionModeChangedCommand ??= new DelegateCommand<object>(ExecuteSelectionModeChangedCommand);/// <summary>/// 模式选择改变事件/// </summary>/// <param name="parameter"></param>private void ExecuteSelectionModeChangedCommand(object parameter){SharedViewModel shared = DI.Resolve<SharedViewModel>();shared.InvokeModeChangedCommand(SelectedModeIndex);}}
3. ViewModel类,构建了listView的数据源
private ObservableCollection<InspectionInfo> _inspectionInfoList;public ObservableCollection<InspectionInfo> InspectionInfoList{get => _inspectionInfoList;set => SetProperty(ref _inspectionInfoList, value);}
4. 页面绑定, ListView 绑定 InspectionInfoList, ComboBox的绑定数据源 ModelList,代码如下:
<ListView Grid.Row="0" Name="ItemList" ItemsSource="{Binding InspectionInfoList}" Background="#515151" SelectedIndex="{Binding SelectedListItemIndex,Mode=TwoWay}" ><ListView.ItemContainerStyle><Style TargetType="ListViewItem"><Setter Property="Margin" Value="0,-40,0,40" /><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="ListBoxItem"><Border Background="{TemplateBinding Background}"><StackPanel ><Grid><Grid.ColumnDefinitions><ColumnDefinition Width="*"/><ColumnDefinition Width="*"/><ColumnDefinition Width="150"/></Grid.ColumnDefinitions><TextBlock Text="{Binding Path=CurrentInspection.RegistrationNo}" Foreground="White" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="left" VerticalAlignment="Center"/><ComboBox Style="{StaticResource ComboBoxInPanelStyle}" ItemsSource="{Binding ModelList}" SelectedIndex="{Binding SelectedModeIndex,Mode=TwoWay}" SelectedValuePath="Name" DisplayMemberPath="Name" Grid.Row="1" Grid.Column="2" SelectedValue="{Binding SelectedValue,Mode=TwoWay}"><be:Interaction.Triggers><be:EventTrigger EventName="SelectionChanged"><prism:InvokeCommandAction Command="{Binding SelectionModeChangedCommand}"/></be:EventTrigger></be:Interaction.Triggers></ComboBox></Grid></StackPanel></Border></ControlTemplate></Setter.Value></Setter><Style.Triggers><Trigger Property="IsSelected" Value="True"><Setter Property="Background" Value="#9C71B9" /><Setter Property="Foreground" Value="white" /></Trigger><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background" Value="#1D1D1D" /><Setter Property="Foreground" Value="white" /></Trigger><Trigger Property="IsEnabled" Value="False"><Setter Property="Background" Value="#757575" /><Setter Property="Foreground" Value="white" /></Trigger></Style.Triggers></Style></ListView.ItemContainerStyle><be:Interaction.Triggers><be:EventTrigger EventName="SelectionChanged"><prism:InvokeCommandAction Command="{Binding Path=ListItemChangedCommand}"/></be:EventTrigger></be:Interaction.Triggers></ListView>
5. 问题是 ComboBox的 SelectedModeIndex 和 SelectionChanged 事件只能在InspectionInfo类中的得到,在ViewModel类中得不到。没办法,只能通过事件,在ViewModel中得到。