c# - RibbonGallery 项目命令单击

标签 c# wpf xaml ribboncontrolslibrary

使用 WPF MVVM 风格。 尝试使用可点击的项目创建 RibbonGallery 由于某种原因,我无法获得启动我的委托(delegate)命令的项目

XAML 代码:

<RibbonMenuButton LargeImageSource="Images/DeleteUser1.png" Label="Delete">
                    <RibbonGallery>
                        <RibbonGalleryCategory ItemsSource="{Binding AvailibleUsers}" Header="User List">
                            <RibbonGalleryCategory.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Image Source="Images/DeleteUser1.png" Width="25"/>
                                        <ContentPresenter Content="{Binding}" Grid.Column="1">
                                            <ContentPresenter.InputBindings>
                                                <MouseBinding MouseAction="LeftClick" Command="{Binding CommandDeleteAllPermissions}"/>
                                            </ContentPresenter.InputBindings>
                                        </ContentPresenter>
                                    </Grid>
                                </DataTemplate>
                            </RibbonGalleryCategory.ItemTemplate>
                        </RibbonGalleryCategory>
                    </RibbonGallery>
                </RibbonMenuButton>

数据上下文已设置为 View 模型。 View 模型:

    public DelegateCommand CommandDeleteAllPermissions { get { return new DelegateCommand(Delegated_DeleteAllPermissions); } }

    private void Delegated_DeleteAllPermissions(object obj)
    {
          \\todo:stuff
    }

我已经使用标准按钮测试了这个命令并且它触发了,但是使用特定的 XAML 代码我无法在我的 RibbonGallery 控件中获得可点击的项目。

有什么想法吗?

最佳答案

画廊是某种分类列表,可以检查其项目。它们适用于当您需要选项菜单时,用户应在其中选中/取消选中项目:

enter image description here

这是用于数据绑定(bind)图库和示例 View 模型的 XAML:

            <RibbonMenuButton Label="FooGallery">
                <RibbonGallery>
                    <RibbonGalleryCategory ItemsSource="{Binding GalleryItems}">
                        <RibbonGalleryCategory.ItemContainerStyle>
                            <Style TargetType="{x:Type RibbonGalleryItem}">
                                <Setter Property="Content" Value="{Binding Content}"/>
                                <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
                            </Style>
                        </RibbonGalleryCategory.ItemContainerStyle>
                    </RibbonGalleryCategory>
                </RibbonGallery>
            </RibbonMenuButton>

这里 GalleryItems 是这些 View 模型的集合:

public class GalleryItem
{
    public object Content { get; set; }

    public bool IsSelected 
    {
        get { return isSelected; }
        set
        {
            if (isSelected != value)
            {
                isSelected = value;

                // TODO: do something here, when item becomes selected/checked; 
                // handle property changing instead of commands

            }
        }
    }

    private bool isSelected;
}

如果您需要下拉菜单来执行一些命令,那么您应该使用常规的RibbonMenuItem。小号:

enter image description here

当菜单项静态已知时,应该这样做:

            <RibbonMenuButton Label="Foo">
                <RibbonMenuItem Header="Bar1" Command="{Binding Bar1Command}"/>
                <RibbonMenuItem Header="Bar2" Command="{Binding Bar2Command}"/>
                <RibbonMenuItem Header="Bar3" Command="{Binding Bar3Command}"/>
            </RibbonMenuButton>

当对菜单项使用 ItemsSource 时,XAML 将如下所示:

            <RibbonMenuButton Label="Foo" ItemsSource="{Binding MenuItems}">
                <RibbonMenuButton.ItemContainerStyle>
                    <Style TargetType="{x:Type RibbonMenuItem}">
                        <Setter Property="Header" Value="{Binding Header}"/>
                        <Setter Property="Command" Value="{Binding Command}"/>
                    </Style>
                </RibbonMenuButton.ItemContainerStyle>
            </RibbonMenuButton>

其中 MenuItems 是这些 View 模型的集合:

public class MenuItemVm
{
    public object Header { get; set; }
    public ICommand Command { get; set; }
}

关于c# - RibbonGallery 项目命令单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31531740/

相关文章:

c# - 使用 xaml 或 C# 创建灯光动画

c# - 在 XAML 应用程序中建立 SQL 连接 - 接下来要采取的步骤

WPF、应用程序设计建议和数据库并发

c# - 根据按下的按钮执行代码

c# - 将 DataGrid 的一行传递给另一个 wpf 表单 c#

c# - 使用哪种语言/工具

javascript - scaleControl 在.net 中无法正确显示

WPF:条件绑定(bind)与属性,XamlParseException 使用

c# - C#中本例中Get A Specific Column Value from a DataTable时如何解决cast not valid?

c# - 在 Windows 窗体中显示插入数据异常