【问题标题】:How to style ContentPresenter in ComboBox?如何在 ComboBox 中设置 ContentPresenter 的样式?
【发布时间】:2013-12-05 22:38:55
【问题描述】:

我目前正在对ComboBox 进行样式设置,使其看起来像 Visual Studio 中的样式(以及颜色主题)。我已经完成了大部分样式设置,但在 ContentPresenter 处停止了显示当前选择的对象。

XAML 部分如下所示:

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

问题是,默认的SelectionBoxItemTemplate 似乎忽略了 ComboBox 的 Foreground 值:

<ComboBox Margin="4" SelectedIndex="0" Foreground="Red">
    <ComboBoxItem>First</ComboBoxItem>
    <ComboBoxItem>Second</ComboBoxItem>
    <ComboBoxItem>Third</ComboBoxItem>                
</ComboBox>

第一个是不可编辑的,SelectionBoxItemTemplate 启动并强制将文本颜色设置为黑色(即使 Foreground 手动设置为红色并且样式设置为另一种非黑色)。

如何将当前的前景色传递给ContentPresenter?如果我不能,我在哪里可以找到SelectionBoxItemTemplate 的工作替代品? (工作,就ComboBox 中的所有项目类型而言,它可以正常工作)

【问题讨论】:

标签: c# wpf xaml combobox styles


【解决方案1】:

我认为它不会忽略 Foreground 颜色。如果您在 XAML 中添加一个(普通)ComboBox 控件,如下所示...:

<ComboBox Foreground="Red">
    <ComboBoxItem>One</ComboBoxItem>
    <ComboBoxItem>Two</ComboBoxItem>
    <ComboBoxItem>Three</ComboBoxItem>
    <ComboBoxItem>Four</ComboBoxItem>
</ComboBox>

...您将看到设置Foreground 确实更改了所选项目的文本。我认为您的问题可能在于您的 ControlTemplate。现在我不能 100% 确定,但我原以为您的 ContentPresenter 应该是这样的:

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

【讨论】:

    猜你喜欢
    • 2014-11-11
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    相关资源
    最近更新 更多