2013-11-22 76 views
0

我目前正在尝试为组合框中选定的项目显示正确的样式。我这样做的原因是,我对ComboBox如何显示所选项目没有太多控制,例如 - 在深色背景下,该项目仍然显示为黑色。根据内容的样式

我想出了以下解决方案:

<DataTemplate x:Key="MyItem" DataType="ComboBoxItem"> 
    <TextBlock Text="{Binding}" Foreground="White"/> 
</DataTemplate> 

<!-- (...) --> 

<Style TargetType="{x:Type ComboBox}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate> 

      <!-- ... --> 
      <!-- Displaying currently selected item --> 

       <ContentPresenter Margin="2" IsHitTestVisible="False" 
        VerticalAlignment="Center" HorizontalAlignment="Stretch" 
        Name="ContentSite" 
        ContentTemplate="{StaticResource MyItem}" 
        Content="{TemplateBinding ComboBox.SelectionBoxItem}" /> 

      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

现在选择了简单ComboBoxItem时,它正确地显示在组合框。另一方面,如果我 - 例如 - 显示带有一些内容的按钮,作为回应,我得到文本System.Windows.Shapes.Rectangle,这与我想要显示的内容远远不同。

我想为ComboBox中显示的不同数据类型使用不同的模板 - 我将能够自定义它们的外观。我怎样才能做到这一点?


编辑:

要非常清楚,我说的是在这种情况下选择(=选择)ComboBox项:

Selected item in combo box

(不是选择组合框的项目在组合框的列表

回答

1
+0

能否请您提供一个小例子,请问有什么可以嵌入该ContentTemplateSelector在我的DataTemplate? – Spook

+0

你看过链接吗? –

+0

我做过,但'ComboBox'似乎没有'ContentTemplateSelector'属性。 – Spook

0

对,这个问题有几个不同的方面。

<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" /> 
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" /> 
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White" /> 
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" /> 

尝试使用这些设置,为他们两个定义文本和两个设置颜色的背景的颜色:首先,我们可以通过添加这个到Resources部分摆脱默认选择颜色选定的项目。事实上,您可以在这里使用任何颜色来提供一种快速方法来更改选定的项目颜色......而不必去解决所有的麻烦。但对于你的例子,让我们把它们作为Transparent

好了,下次你想有一个不同的外观出现在ComboBox每个不同的数据类型......这也可以轻松实现。所有你需要做的是要申报每个类型DataTemplate不设置x:Key属性,使他们将被隐式应用。这是一个基本的例子:

<Window.Resources> 
    <DataTemplate DataType="{x:Type Type1}"> 
     <TextBlock Text="{Binding}" Foreground="Red" /> 
    </DataTemplate> 
    <DataTemplate DataType="{x:Type Type2}"> 
     <TextBlock Text="{Binding}" Foreground="Green" /> 
    </DataTemplate> 
    <DataTemplate DataType="{x:Type Type3}"> 
     <TextBlock Text="{Binding}" Foreground="Blue" /> 
    </DataTemplate> 
</Window.Resources> 

如果用这三种这些DataTemplate S的物品填充的ComboBox,那么项目将在Red有色,绿色and Blue`。

+0

你确定,你说的是同样的“选定的项目”我吗?我修改了我的问题一点,以显示,我在说什么 – Spook

+0

你是对,我确实认为你在讨论* actual *选择的项目,但是,在显示数据类型实例的任何地方,单独的DataTemplate应该仍然可以工作。 – Sheridan

0

我会留下ContentTemplateSelector所有,谁寻求同样的问题,解决的方案。

  1. 添加下面的类到您的库/应用:

    public class ComboBoxSelectionTemplateSelector : DataTemplateSelector 
    { 
    
        public DataTemplate TextTemplate 
        { 
         get; 
         set; 
        } 
    
        public DataTemplate ContentTemplate 
        { 
         get; 
         set; 
        } 
    
        public override System.Windows.DataTemplate SelectTemplate(object item, DependencyObject container) 
        { 
         if (item is string) 
          return TextTemplate; 
         else 
          return ContentTemplate; 
        } 
    } 
    
  2. 定义两个不同的DataTemplates:

    <DataTemplate x:Key="ComboBoxTextItem"> 
        <TextBlock Text="{Binding}" Foreground="{DynamicResource {x:Static vs:VsBrushes.WindowTextKey}}" /> 
    </DataTemplate> 
    
    <DataTemplate x:Key="ComboBoxOtherItem"> 
        <ContentPresenter Content="{Binding}" /> 
    </DataTemplate> 
    
  3. 实例化要在使用它里面的风格选择:

    <Style.Resources> 
        <local:ComboBoxSelectionTemplateSelector ContentTemplate="{StaticResource ComboBoxOtherItem}" 
                  TextTemplate="{StaticResource ComboBoxTextItem}" x:Key="ContentTemplateSelector" /> 
    </Style.Resources> 
    
  4. ContentPresenter使用它:

    <!-- ... --> 
    <ControlTemplate TargetType="ComboBox"> 
        <Grid> 
         <ContentPresenter ContentTemplateSelector="{StaticResource ContentTemplateSelector}" 
              Content="{TemplateBinding ComboBox.SelectionBoxItem}" /> 
         <!-- Other items --> 
         <Popup ... /> <!-- For displaying ComboBox's list --> 
        </Grid>