如何将Ribbon Gallery Category正确绑定到集合

时间:2013-02-21 13:44:13

标签: xaml data-binding ribbon

我正在使用Microsoft.Windows.Controls.Ribbon。

我希望在我的功能区中有一个带有图片按钮的动态组合框。

如果我直接在xaml中这样做,我会得到我想要的东西:

<ribbon:RibbonComboBox 
                      SelectionBoxWidth="62"
                      VerticalAlignment="Center" 
                      >
                    <ribbon:RibbonGallery SelectedValue="0"
                      SelectedValuePath="Content"
                      MaxColumnCount="1">
                        <ribbon:RibbonGalleryCategory>
                            <ribbon:RibbonButton Label="Histo"  HorizontalContentAlignment="Stretch"
                                       Command="{Binding NewHistogrammCommand}" 
                                       SmallImageSource="/Test;component/Resourcen/Histogramm32.png" 
                                       LargeImageSource="/Test;component/Resourcen/Histogramm32.png" />
                            <ribbon:RibbonButton Label="3D"  HorizontalContentAlignment="Stretch"
                                       Command="{Binding NewDreiDCommand}" 
                                       SmallImageSource="/Test;component/Resourcen/DreiD32.png" 
                                       LargeImageSource="/Test;component/Resourcen/DreiD32.png" />
                        </ribbon:RibbonGalleryCategory>
                    </ribbon:RibbonGallery>
                </ribbon:RibbonComboBox>

但如果我尝试通过这种方式绑定到集合来实现这一点:

                    <ribbon:RibbonComboBox 
                      SelectionBoxWidth="62"
                      VerticalAlignment="Center" 
                      IsEditable="True" >
                    <ribbon:RibbonGallery
                      MaxColumnCount="1">
                        <ribbon:RibbonGalleryCategory ItemsSource="{Binding LayoutContentTypeList, ElementName=mainWindow}">
                            <ribbon:RibbonGalleryCategory.ItemTemplate>
                                <DataTemplate>
                                    <ribbon:RibbonButton Label="{Binding Header}" HorizontalContentAlignment="Stretch"
                                           Command="{Binding Command}" 
                                           CommandParameter="{Binding CommandParameter}" 
                                           SmallImageSource="{Binding ImageSource}"
                                           LargeImageSource="{Binding ImageSource}" />
                                </DataTemplate>
                            </ribbon:RibbonGalleryCategory.ItemTemplate>
                        </ribbon:RibbonGalleryCategory>
                    </ribbon:RibbonGallery>
                </ribbon:RibbonComboBox>

我得到了

  

System.Windows.Data错误:40:BindingExpression路径错误:'object'''ContentPresenter'(Name ='')'上找不到'IsDropDownOpen'属性。 BindingExpression:路径= IsDropDownOpen; DataItem ='ContentPresenter'(Name =''); target元素是'RibbonButton'(Name =''); target属性是'NoTarget'(类型'Object')

按钮工作正常,但如何解决此绑定错误?

1 个答案:

答案 0 :(得分:1)

我猜你已经为你的问题找到了解决方案,但对于遇到这篇文章的其他人,寻找答案,你可以找到一个完整的解决方案,你可以在{{ {3}}发布在“Windows Presentation Foundation团队的官方博客”上。基本思路如下。

您设置为RibbonGallery.DataContext的任何对象都应该具有绑定到RibbonGalleryCategory.ItemsSource属性的集合属性。该集合中的对象应具有包含要在库项目中显示的值的属性。为HierarchicalDataTemplate声明RibbonGallery.CategoryTemplate以绑定您的媒体资源。以下是链接帖子的示例:

<Ribbon:RibbonGallery DataContext="{x:Static data:WordModel.StylesParagraphGalleryData}" 
ItemsSource="{Binding CategoryDataCollection}" 
ScrollViewer.VerticalScrollBarVisibility="Hidden">
    <Ribbon:RibbonGallery.CategoryTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding GalleryItemDataCollection}">
            <Border Background="LightGray">
                <TextBlock Text="{Binding}" FontWeight="Bold" />
            </Border>
            <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Source="Images\Paragraph_32x32.png" />
                        <TextBlock Margin="10,0,0,0" Text="{Binding}" />
                    </StackPanel>
                </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </Ribbon:RibbonGallery.CategoryTemplate>
</Ribbon:RibbonGallery>