如何正确绑定 RibbonGalleryCategory 到一个集合。

3
我将使用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 Error: 40 : BindingExpression path error: 'IsDropDownOpen' property not found on 'object' ''ContentPresenter' (Name='')'. BindingExpression:Path=IsDropDownOpen; DataItem='ContentPresenter' (Name=''); target element is 'RibbonButton' (Name=''); target property is 'NoTarget' (type 'Object')

按钮功能正常,但我该如何解决这个绑定错误?

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接