2015-08-08 75 views
0

我已經在ListView定義了以下資源:從靜態資源創建comboboxitem

<local:FillPatternDefinition x:Key="deleteItem" TypeName="Delete Regions" ItsId="-1"/> 

,並通過這種方式定義列表視圖託管組合框:

<ComboBox 
     Name="changeComboBox" 
     Width="100" 
     DisplayMemberPath="TypeName" 
     <ComboBox.ItemsSource> 
      <CompositeCollection> 
       <ComboBoxItem Foreground="Black" Background="Salmon" Content="{StaticResource deleteItem}"/> 
       <CollectionContainer Collection="{Binding Source={StaticResource theComboBoxDataView}}" /> 
      </CompositeCollection> 
     </ComboBox.ItemsSource> 
</ComboBox> 

問題是comboxitem在複合集合中。在下拉框中,我看到類名(FillPatternDefinition),但是當我選擇它時,類型名稱「刪除區域」在組合框中正確顯示。 collectioncontainer擁有相同的類別,但所有項目均顯示並正常工作。

我是否需要將靜態資源包裝在其他某種類中以使其在下拉列表中正常工作?

回答

0

您不應該在CompositeCollection中顯式創建ComboBoxItem,因爲ComboBox內部會爲其ItemsSource集合中的每個元素創建ComboBoxItems。使用StaticResource代替:

<ComboBox.ItemsSource> 
    <CompositeCollection> 
     <StaticResourceExtension ResourceKey="deleteItem"/> 
     <CollectionContainer 
      Collection="{Binding Source={StaticResource theComboBoxDataView}}" /> 
    </CompositeCollection> 
</ComboBox.ItemsSource> 

設置的第一個項目的ForegroundBackground在ComboBox的ItemContainerStyle可以做。