gpt4 book ai didi

c# - 在 WPF 中将命令绑定(bind)到 ComboBoxItem

转载 作者:太空狗 更新时间:2023-10-29 23:52:14 25 4
gpt4 key购买 nike

我有一个简单的组合框,我想在每次所选值更改时触发单独的命令。这是我的标记示例:

<WrapPanel Grid.Row="0" Visibility="{Binding ShowToggleViewFeedViewManual}">
<ComboBox Margin="3,3,0,0">
<ComboBoxItem IsEnabled="{Binding CanSelectViewFeedData}" >
<ComboBoxItem.CommandBindings>
<CommandBinding Command="SelectViewFeedDataCommand" />
</ComboBoxItem.CommandBindings>
<TextBlock Text="View Feed Data"/>
</ComboBoxItem>
<ComboBoxItem IsEnabled="{Binding CanSelectViewManualData}">
<ComboBoxItem.CommandBindings>
<CommandBinding Command="SelectManualFeedDataCommand" />
</ComboBoxItem.CommandBindings>
<TextBlock Text="View Manual Data"/>
</ComboBoxItem>
</ComboBox>
</WrapPanel>

我收到一条错误消息“无法转换‘SelectViewFeedDataCommand’”。对于其他 ComboBoxItem,我也收到类似的错误。 ICommand 在 ViewModel 类中定义,该类是 UserControl 的数据源,绑定(bind)为 DataTemplate。

public ICommand SelectViewFeedDataCommand
{
get
{
// Code to perform
}
}

我对此进行了相当广泛的研究,但没有找到如何有效地将 ICommand 绑定(bind)到 ComboBoxItem 的答案。

我正在从使用一组单选按钮和相关命令的现有代码中改编它,这很容易完成。使用 ComboBox 没有简单的方法来做到这一点吗?

谢谢。

最佳答案

您是否尝试过将命令放入绑定(bind)?

<CommandBinding Command="{Binding SelectManualFeedDataCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />

编辑更新策略:

由于 ComboBox 项不支持直接命令绑定(bind),请尝试创建附加属性:

Link

关于c# - 在 WPF 中将命令绑定(bind)到 ComboBoxItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11615334/

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