XAML/WPF:如何将命令绑定到组合框项

发布于 2024-08-16 19:26:14 字数 1116 浏览 2 评论 0原文

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

编辑:

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>

I want to bind a command to a comboboxitem much like a typical command (either application or custom) is bound to a button. I can't seem to find an example.

EDIT:

The ComboBoxItem exists as part of a ContentControl that also contains a button. The button has an associated command that works well. How can I bind a command, in a similar way, to the 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宫墨修音 2024-08-23 19:26:14
<ComboBox>
    <ComboBox.Resources>
        <Style TargetType="ComboBoxItem">
            <EventSetter Event="Foo" Handler="Bar"/>
        </Style>
    </ComboBox.Resources>
</ComboBox>
<ComboBox>
    <ComboBox.Resources>
        <Style TargetType="ComboBoxItem">
            <EventSetter Event="Foo" Handler="Bar"/>
        </Style>
    </ComboBox.Resources>
</ComboBox>
起风了 2024-08-23 19:26:14

作为选项之一,您可以考虑将按钮放入组合框的 ItemTemplate 中,并更改按钮的模板以使其看起来“根本不像按钮”。

但那很脏。也许您可以使用菜单而不是组合框?

As one of the options you can consider putting a Button into the ComboBox's ItemTemplate and change the Button's template to make it look "not like a button at all".

But that is dirty. Maybe you can use Menu instead of ComboBox?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文