2015-08-08 53 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可以做。