2013-02-21 28 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>