我有一个 ComboBox
的以下 XAML,它有一个代码隐藏 SelectionChanged
事件处理程序和 ViewModel 的另一个 Command 属性。我已将 SelectedIndex
属性设置为 0。现在,当我运行该项目时,会调用代码隐藏处理程序,但 Command
不会执行。我想要的是第一次加载 View 时应为 SelectedIndex=0
执行 Command
。
<ComboBox Name="listComboBox" SelectionChanged="listComboBox_SelectionChanged" SelectedIndex="0" SelectedValuePath="Content" Margin="5,0" Height="35" Width="150" VerticalAlignment="Center" HorizontalAlignment="Left">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding ListTypeComboSelectionChangedCmd}" CommandParameter="{Binding ElementName=listComboBox, Path=SelectedValue}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<ComboBoxItem Content="ItemOne" />
<ComboBoxItem Content="ItemTwo" />
<ComboBoxItem Content="ItemThree" />
</ComboBox>
更新
代码隐藏事件处理程序:
private void listComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { }
ICommand 对象:
public ICommand ListTypeComboSelectionChangedCmd
{
get { return new RelayCommand<string>(ListTypeComboSelectionChangedCmdExec); }
private set;
}
ICommand 处理程序:
private void ListTypeComboSelectionChangedCmdExec(string listType) { }
最佳答案
将 SelectedValue
绑定(bind)到 View 模型上的 Property
。
在 Property
set{...}
block 中执行逻辑或调用
ListTypeComboSelectionChangedCmdExec(value)
关于WPF ComboBox SelectionChanged 事件命令未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22619114/