【发布时间】: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 中的所有项目类型而言,它可以正常工作)
【问题讨论】:
-
今天正好有另一个用户问了同样的问题。与其将所有内容复制粘贴到此处,我建议您在此处阅读我的答案:stackoverflow.com/questions/20116418/… 这是如何解决这些样式继承问题的教程的链接:blogs.msdn.com/b/wpfsdk/archive/2009/08/27/…
标签: c# wpf xaml combobox styles