wpf 如何知道在 DropDownButton 中点击了什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23019282/
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 11:30:39  来源:igfitidea点击:

How to know what gets clicked in a DropDownButton

c#wpfmahapps.metro

提问by Joel Lucsy

Per this page, the DropDownButton is using a ContextMenu to display the ItemsSource. How are we to know what the user clicks on? The Click event on the button isn't for the menu, but the button itself. I see no other events on it.

根据此页面,DropDownButton 使用 ContextMenu 来显示 ItemsSource。我们如何知道用户点击了什么?按钮上的 Click 事件不是针对菜单的,而是针对按钮本身的。我看不到其他事件。

回答by akagixxer

I came across this question looking for the same answer. I never really found anything online but discovered this solution on my own. Perhaps it will help someone in the future.

我遇到了这个问题,正在寻找相同的答案。我从来没有真正在网上找到任何东西,而是自己发现了这个解决方案。也许它会在未来帮助某人。

As stated previously the DropDownButtonuses a ContextMenuto display its ItemsSource. Basically what I was looking for was a "Menu-like" drop down coming from a button. For example, say you have a DropDownButtonthat says "Add". Maybe you want 2 options like "Add New" and "Add Existing". So this is what I did...

如前所述,DropDownButton使用 aContextMenu来显示其ItemsSource. 基本上我正在寻找的是来自按钮的“类似菜单”的下拉菜单。例如,假设您有一个DropDownButton“添加”。也许您需要 2 个选项,例如“添加新的”和“添加现有的”。所以这就是我所做的......

First I made some object to hold the header/content and the command.

首先,我创建了一些对象来保存标题/内容和命令。

public class TitledCommand
{
    public String Title { get; set; }
    public ICommand Command { get; set; }
}

Theoretically you would have a list of these to bind to the ItemsSourceof the DropDownButton.

理论上你必须绑定到这些名单ItemsSourceDropDownButton

public List<TitledCommand> TitledCommmands { get; private set; }

Now we just style the item container for the DropDownButtonso it picks up the header and command from our objects in the ItemsSource.

现在我们只是为 设置项目容器的样式,DropDownButton以便它从ItemsSource.

Include MahApps:

包括 MahApps:

xmlns:metroControls="http://metro.mahapps.com/winfx/xaml/controls"

And here is the style...

这是风格......

<metroControls:DropDownButton Content="Add" ItemsSource="{Binding Path=TitledCommmands}">
    <metroControls:DropDownButton.ItemContainerStyle>        
        <Style TargetType="MenuItem">
            <Setter Property="Header" Value="{Binding Path=Title}"/>
            <Setter Property="Command" Value="{Binding Path=Command}"/>
        </Style>          
    </metroControls:DropDownButton.ItemContainerStyle>
</metroControls:DropDownButton>

回答by Aleksej

You can override the item template for the control and can add a handler inside of it like this:

您可以覆盖控件的项模板,并可以在其中添加一个处理程序,如下所示:

<controls:DropDownButton Content="Select me" x:Name="selectMeDropDownButton">
    <controls:DropDownButton.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" MouseDown="selectMeDropDownButton_TextBlock_MouseDown" />
        </DataTemplate>
    </controls:DropDownButton.ItemTemplate>
</controls:DropDownButton>

And implement the event handler in code-behind file like this:

并在代码隐藏文件中实现事件处理程序,如下所示:

void selectMeDropDownButton_TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.ChangedButton == MouseButton.Left && this.selectMeDropDownButton.IsExpanded)
    {
        var value = ((TextBlock)e.Source).DataContext;
        // Do something meaningful with the value, it's an item from ItemsSource
    }
}

The check for DropDownButton.IsExpandedis necessary as the same ItemTemplateis applied to the Contenton the button itself. Of course you can replace TextBlock with any Control/UIElementyou like.

检查 forDropDownButton.IsExpanded是必要的,因为同样ItemTemplate适用于Content按钮本身。当然,您可以用任何您喜欢的Control/替换 TextBlock UIElement

回答by 123 456 789 0

Create an attached propertyto the ContextMenu/DropDownButton (whichever you prefer). If you did dropdown then get the context menu that it is showing then hook up the Clickevents from it and push the value back to the property.

创建一个attached property到 ContextMenu/DropDownButton(无论你喜欢哪个)。如果您做了下拉菜单,则获取它显示的上下文菜单,然后从中连接Click事件并将值推回属性。

回答by Morty

Use SplitButtoninstead of DropDownButton. The first has SelectionChanged event.

使用SplitButton而不是 DropDownButton。第一个有 SelectionChanged 事件。