在 WPF 中将命令绑定到 ComboBoxItem

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11615334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:40:42  来源:igfitidea点击:

Binding Commands to ComboBoxItem in WPF

c#wpfcombobox

提问by jpaull

I have a simple ComboBox that I want to trigger separate commands each time the selected value changes. Here is an example of my markup:

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

<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>   

I get an error stating "Cannot convert 'SelectViewFeedDataCommand'". I get a similar error for the other ComboBoxItem as well. The ICommand is defined in the ViewModel Class that is the DataSource for the UserControl, bound as a DataTemplate.

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

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

I have researched this pretty extensively but haven't found an answer for how to effectively bind the ICommand to the ComboBoxItem.

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

I am adapting this from existing code that used a set of radiobuttons and associated commands, which is done pretty easily. Is there no simple way to do this with a ComboBox?

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

Thanks.

谢谢。

采纳答案by jpaull

I was looking at my old posts and realized that I never posted the solution to my question... pretty simple really (if not elegant). I just created a List in my ViewModel that contained my Combo Box "choices" and then when the SelectedValue was changed, triggered my "changed" logic in the setter for the property:

我正在查看我的旧帖子并意识到我从未发布过我的问题的解决方案......真的很简单(如果不优雅的话)。我刚刚在我的 ViewModel 中创建了一个包含我的组合框“选择”的列表,然后当 SelectedValue 更改时,在属性的 setter 中触发了我的“更改”逻辑:

<WrapPanel Grid.Row="0" Visibility="{Binding ShowToggleFeedViewManual}" >
    <ComboBox Margin="10,10,10,10" Width="200" ItemsSource="{Binding AvailableDataSources}" SelectedValue="{Binding SelectedDataSource}"/>
</WrapPanel>

And from the view-model:

从视图模型:

public FeedSource SelectedDataSource
    {
        get { return _selectedDataSource; }
        set
        {
            _selectedDataSource = value;
            base.OnPropertyChanged("SelectedDataSource");

            //additional code to perform here

        }
    }

回答by Josh

Have you tried putting the command to a binding?

您是否尝试将命令置于绑定中?

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

Edit for updated strategy:

编辑更新策略:

Since the ComboBox item doesn't support direct command binding, try creating an attached property:

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

Link

关联