2013-02-21 49 views
1

我正在使用Microsoft.Windows.Controls.Ribbon。如何正确地将RibbonGalleryCategory绑定到集合

我想在我的丝带上有一个带图片按钮的动态组合框。

如果我这样做,直接在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

我在猜测你已经找到了解决问题的方法,但是对于其他人来说这个帖子,寻找答案,你可以找到一个完整的解决方案,你可以下载和审查你的休闲'Windows Presentation Foundation团队的官方博客'发布于How do I add Galleries to my Ribbon?。基本思路如下。

无论您设置为RibbonGallery.DataContext的对象是否应具有用于绑定到RibbonGalleryCategory.ItemsSource属性的集合属性。该集合中的对象应具有包含要显示在图库项目中的值的属性。为RibbonGallery.CategoryTemplate声明HierarchicalDataTemplate以绑定您的属性。以下是链接帖子的示例:

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