【问题标题】:XAML/WPF: How do I bind a command to a comboboxitemXAML/WPF:如何将命令绑定到组合框项
【发布时间】:2010-01-06 11:33:04
【问题描述】:

我想将命令绑定到组合框项,就像将典型命令(应用程序或自定义)绑定到按钮一样。我似乎找不到一个例子。

编辑:

ComboBoxItem 作为 ContentControl 的一部分存在,该控件还包含一个按钮。该按钮有一个运行良好的关联命令。如何以类似的方式将命令绑定到 ComboBoxItem?

<!--    Line    -->
<Button x:Name="Line"
    Style="{DynamicResource Button_Title}"
    Grid.Row="1"
    Grid.RowSpan="3"
    Grid.Column="0"
    Content="Line"
    Command="{x:Static local:Ribbon_AC.Custom_RoutedUICommand}"
    CommandParameter="Line"
    Tag="{DynamicResource Line_32}"/>

    <!--    Arc -->
    <ContentControl x:Name="ArcSplit"
            Template="{DynamicResource Control_SplitSmall}"
            Tag="{DynamicResource Arc_20}"
            Grid.Row="1"
            Grid.Column="1">

    <ComboBox Name="ComboBox_Arc"
        Style="{DynamicResource ComboBox_Small}"
        Width="{DynamicResource Width_DropDown}">

        <!--    Arc_0   -->
        <ComboBoxItem   x:Name="Arc_0"
                Style="{DynamicResource ComboBoxItem_Large}"
                Tag="{DynamicResource Arc0_32}"
                Content="Arc 0">

        </ComboBoxItem>

【问题讨论】:

  • 您希望命令何时执行?是希望每次选中项发生变化时都执行命令,还是希望能够在不触发命令的情况下更改选中项?
  • 它是基于 ComboBox 的模板化拆分按钮的一部分。我希望它在单击时执行以及更改拆分上的图像,以便我下次只需单击按钮即可重新发出命令。类似于为电子表格中的单元格选择背景颜色。

标签: wpf xaml binding combobox command


【解决方案1】:
<ComboBox>
    <ComboBox.Resources>
        <Style TargetType="ComboBoxItem">
            <EventSetter Event="Foo" Handler="Bar"/>
        </Style>
    </ComboBox.Resources>
</ComboBox>

【讨论】:

  • 谢谢食人族。原来这是一个范围问题。
【解决方案2】:

作为其中一种选择,您可以考虑将 Button 放入 ComboBox 的 ItemTemplate 并更改 Button 的模板以使其看起来“根本不像按钮”。

但这很脏。也许您可以使用 Menu 来代替 ComboBox?

【讨论】:

  • 我试图做到这一点,但还有其他考虑。我认为您的菜单创意可能最终会成为可行的方法,再次,出于多种原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-12
  • 1970-01-01
  • 1970-01-01
  • 2013-10-04
相关资源
最近更新 更多