gpt4 book ai didi

WPF ComboBox SelectionChanged 事件命令未触发

转载 作者:行者123 更新时间:2023-12-01 18:37:44 27 4
gpt4 key购买 nike

我有一个 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)

参见Binding ComboBox SelectedItem using MVVM

关于WPF ComboBox SelectionChanged 事件命令未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22619114/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com