gpt4 book ai didi

wpf - 当组合框位于 ListView 单元格中时如何通过委托(delegate)命令处理 SelectionChanged 事件

转载 作者:行者123 更新时间:2023-12-04 14:38:48 24 4
gpt4 key购买 nike

WPF command support in ComboBox”,此页面展示了如何扩展组合框以支持命令,但它没有给出映射到组合框的 SelectedIndexChanged 事件的委托(delegate)命令的圆顶。现在我面临的问题是如何处理组合框 SelectedIndexChanged 事件,就像它是一次性组合框的情况一样:

<ComboBox SelectionChanged="ComboBox_SelectionChanged"></ComboBox>
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var combobox = sender as ComboBox;
if (combobox.SelectedIndex == 0)
{
//todo:
}
}

目前情况如下:

<GridViewColumn.CellTemplate>
<DataTemplate>
<Ext:CommandSupportedComboBox SelectedIndex="{Binding StartMode}"
Command="{Binding ChangeStartModeCmd}">
<ComboBoxItem>Automatically</ComboBoxItem>
<ComboBoxItem>Manual</ComboBoxItem>
<ComboBoxItem>Forbidden</ComboBoxItem>
</Ext:CommandSupportedComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>

/// <summary>
/// change service start mode command
/// </summary>
public ICommand ChangeStartModeCmd { get; private set; }

和相应的委托(delegate)方法:

/// <summary>
/// change service start mode
/// </summary>
public void ChangeStartMode()
{
//todo:
}

命令绑定(bind)方法:

ChangeStartModeCmd = new DelegateCommand(ChangeStartMode);

我想这样定义方法:

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var combobox = sender as ComboBox;
if (combobox.SelectedIndex == 0)
{
//todo:
}
}

但如何将它绑定(bind)到委托(delegate)命令 ChangeStartModeCmd?

ChangeStartModeCmd = new DelegateCommand(ChangeStartMode( 
/*what should I pass for the method?*/));

最佳答案

您可能不需要 CommandSupportedCombobox,因为您可以附加 ComboBox 的 SelectedItem 属性,并在 ViewModel 的 setter 中调用函数想要...

Xaml

<ComboBox SelectedItem="{Binding MyItem,Mode=TwoWay}" />

View 模型

 public MyItem
{
get {return myItem;}
set
{
myItem=value;
OnPropertyChanged("MyItem"); implement INotifyPropertyChanged
MyFavFunction(); // Function to be called
}
}

关于wpf - 当组合框位于 ListView 单元格中时如何通过委托(delegate)命令处理 SelectionChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10274637/

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