简体   繁体   English

将命令绑定到WPF中的ComboBoxItem

[英]Binding Commands to ComboBoxItem in WPF

I have a simple ComboBox that I want to trigger separate commands each time the selected value changes. 我有一个简单的ComboBox,我希望每次选定的值更改时触发单独的命令。 Here is an example of my markup: 这是我的标记示例:

<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' ". 我收到一条错误,指出“ 无法转换'SelectViewFeedDataCommand' ”。 I get a similar error for the other ComboBoxItem as well. 我也得到了另一个ComboBoxItem的类似错误。 The ICommand is defined in the ViewModel Class that is the DataSource for the UserControl, bound as a DataTemplate. ICommand在ViewModel类中定义,该类是UserControl的DataSource,绑定为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. 我正在从使用一组radiobuttons和相关命令的现有代码中进行调整,这很容易完成。 Is there no simple way to do this with a ComboBox? 使用ComboBox没有简单的方法吗?

Thanks. 谢谢。

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

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中创建了一个List,其中包含我的Combo Box“选项”,然后当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

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM