c# - RibbonMenuButton 绑定(bind)项目单击以执行命令

标签 c# wpf xaml data-binding ribbon-control

我的 RibbonMenuButton 有问题。目前我有:

<RibbonMenuButton Label="Meeting" Width="Auto" ToolTipDescription="Display requests on the agenda for the meeting selected" ToolTipTitle="Meeting" 
                                  LargeImageSource="pack://application:,,,/Resources/meeting.png"
                                  ItemsSource="{Binding MeetingsAvailable}">                       

    <RibbonMenuButton.ItemTemplate>                            
        <DataTemplate>
            <TextBlock Text="{Binding Value}"/>
        </DataTemplate>
    </RibbonMenuButton.ItemTemplate>                                         
</RibbonMenuButton>

我的MeetingsAvailable实际上是Dictionary<int, string> 。此代码正在运行,RibbonMenuButton很好地显示了每个 Value字典的。

现在我正在尝试取回 Key MenuItem的已被点击。我的想法是使用 ICommand在我的 ViewModel 中并将事件绑定(bind)到此命令。但我真的不知道如何获取与点击 RibbonMenuButton 中的某个项目对应的事件

有人已经这样做了吗?

提前谢谢您。

最佳答案

您可以使用 ItemContainerStyle 属性将 ICommand 数据绑定(bind)到 RibbonMenuButton,如下所示:

<RibbonMenuButton Label="Meeting" ItemsSource="{Binding MeetingsAvailable}" ... >
    <RibbonMenuButton.ItemTemplate>                            
        <DataTemplate>
            <TextBlock Text="{Binding Value}"/>
        </DataTemplate>
    </RibbonMenuButton.ItemTemplate>
    <RibbonMenuButton.ItemContainerStyle>
        <Style TargetType="{x:Type MenuItem}">
            <Setter Property="Command" Value="{Binding DataContext.NameOfCommand,
                RelativeSource={RelativeSource AncestorType={x:Type Views:View}}}" />
            <Setter Property="CommandParameter" Value="{Binding Key}" />
        </Style>
    </RibbonMenuButton.ItemContainerStyle>                                         
</RibbonMenuButton>

关于c# - RibbonMenuButton 绑定(bind)项目单击以执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24202376/

相关文章:

C# 事件,那个 null 的东西是怎么回事?

c# - 如何以编程方式为 'AutoDetect' 连接模式配置 Azure ServiceBus 监听器?

c# - 在 WPF 中进行 DataBound 时将 TextBlock 设置为完全粗体

c# - 如何更改 TabControl.Header 的背景颜色

c# - 动态创建的网格不显示

c# - 可以在文件列表上运行的 C/C++ 和 C# 语法检查器

c# - 使用 URI 类时保持 url 编码

.net - WPF 将集合绑定(bind)到 ComboBox 并选择一个项目

wpf - 标签 'Interaction.Behaviors' 在 VS2010 (Blend 3) 中不存在

wpf - 在XAML中定义 View /演示者对的语法是什么?