gpt4 book ai didi

c# - 如何知道在 DropDownButton 中点击了什么

转载 作者:可可西里 更新时间:2023-11-01 08:41:20 24 4
gpt4 key购买 nike

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

最佳答案

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

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

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

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

理论上,您会有一个列表,这些列表将绑定(bind)到 DropDownButtonItemsSource

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

现在我们只需为 DropDownButton 设置项容器的样式,以便它从 ItemsSource 中的对象中获取标题和命令。

包括 MahApps:

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

这是样式...

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

关于c# - 如何知道在 DropDownButton 中点击了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23019282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com