RibbonGallery项目命令单击

时间:2015-07-21 06:23:04

标签: c# wpf xaml ribboncontrolslibrary

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

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>

datacontext已设置为视图模型。 视图模型:

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

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

我已使用标准按钮测试此命令并触发,但使用特定的XAML代码我无法在RibbonGallery控件中获取可点击的项目。

任何想法?

2 个答案:

答案 0 :(得分:3)

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

enter image description here

这是数据绑定图库和示例视图模型的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是这些视图模型的集合:

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

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是这些视图模型的集合:

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

答案 1 :(得分:0)

看我想绑定项目来源。因为项目来源经常变化。 这样做我必须将我的字符串集合修改为自定义对象,并让每个对象都包含ICommand或其他对象。这也需要让ViewModel程序员改变他的东西。

因为我只是要改变观点。

我只是这样做了

              <RibbonMenuButton LargeImageSource="Images/DeleteUser1.png" Label="Delete" ItemsSource="{Binding AvailibleUsers}">
                    <RibbonGallery Command="{Binding CommandDeleteAllPermissions}">
                        <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"/>
                                    </Grid>
                                </DataTemplate>
                            </RibbonGalleryCategory.ItemTemplate>
                        </RibbonGalleryCategory>
                    </RibbonGallery>
                </RibbonMenuButton>

我只需要将命令添加到此处

<RibbonGallery Command="{Binding CommandDeleteAllPermissions}">

现在,图库中的所有项目在点击时都会触发命令。

感谢大家的帮助