使用TemplateBinding绑定WPF ComboBoxItem ItemTemplate的颜色时遇到的困难
我正在尝试实现一个简单的组合框(绑定到字符串列表),它在右侧显示每个项目作为字符串标签,在左侧显示图标(以几何图形的形式)。我希望框中的项目继承我正在使用的颜色,因为我有一个自定义方案(带有深色背景和白色文本) 我能够使用TemplateBinding(绑定到ComboBoxItem.Foreground)使文本标签的颜色正确显示,但无法对几何图形执行相同操作,我不知道为什么 这是一幅我正在努力实现的图像 请注意,图标很好地显示在名称的左侧。仅当我明确指定“白色”作为几何图形的画笔时,此选项才有效 下面是我用来完成这项工作的XAML使用TemplateBinding绑定WPF ComboBoxItem ItemTemplate的颜色时遇到的困难,wpf,xaml,Wpf,Xaml,我正在尝试实现一个简单的组合框(绑定到字符串列表),它在右侧显示每个项目作为字符串标签,在左侧显示图标(以几何图形的形式)。我希望框中的项目继承我正在使用的颜色,因为我有一个自定义方案(带有深色背景和白色文本) 我能够使用TemplateBinding(绑定到ComboBoxItem.Foreground)使文本标签的颜色正确显示,但无法对几何图形执行相同操作,我不知道为什么 这是一幅我正在努力实现的图像 请注意,图标很好地显示在名称的左侧。仅当我明确指定“白色”作为几何图形的画笔时,此选项才
<tk:RadComboBox ItemsSource="{Binding PostAnalysisRoutines}"
Grid.Row="8" Grid.Column="2"
Text="{Binding Settings.PostCaptureAnalysisRoutine, Mode=TwoWay}">
<!--
Each item in the box is a horizontal stackpanel with an icon for
the routine on the left and the routine name on the right
-->
<tk:RadComboBox.ItemTemplate>
<DataTemplate DataType="sdk:AnalysisRoutine">
<StackPanel Orientation="Horizontal">
<Image Width="30" Height="30">
<Image.Source>
<DrawingImage>
<DrawingImage.Drawing>
<!-- ***FORCE TO USE "White" here WHY???*** -->
<GeometryDrawing Brush="White"
Geometry="{Binding Converter={StaticResource PathGeometryConverter}}"
/>
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>
</Image>
<!-- *** BUT ITS OK TO USE TemplateBinding HERE. WHY??? *** -->
<Label Foreground="{TemplateBinding tk:RadComboBoxItem.Foreground}"
Background="{TemplateBinding tk:RadComboBoxItem.Background}"
Content="{Binding}"/>
</StackPanel>
</DataTemplate>
</tk:RadComboBox.ItemTemplate>
</tk:RadComboBox>
几点注意:
- 我在这里使用Telerik的RadComboBox,但如果我将其切换到常规WPF ComboBox,我会看到类似的问题
- 我能够对标签的前景色和背景色使用TemplateBindings绑定到ComboBoxItem。这些很好用
- PathGeometryConverter只接受字符串名称并为其查找预定义的PathGeometry资源李>
<GeometryDrawing Brush="{TemplateBinding tk:RadComboBoxItem.Foreground}"
Geometry="{Binding Converter={StaticResource PathGeometryConverter}}"
/>
我的结论是:
因此,我被迫明确指定“白色”作为笔刷的颜色
绘制几何图形来完成此工作
让我的模板中的几何图形“继承”周围的配色方案的正确方法是什么。。。?我是否缺少某种模板绑定?我认为您的问题是因为您使用的模板绑定与来自资源的内容相结合,或者是因为它比被模板化的内容低了几个级别。如果几何体直接在模板中,那么我认为它会起作用 我建议您改为尝试使用relativesource和ancestortype tk:RadComboBoxItem进行绑定,路径=前景。 或相对资源模板的父对象
我也会使用路径而不是图像。在这种情况下,可能差别不大,但路径可能更为清晰。可以将路径的数据绑定到几何图形资源。或者至少我认为您可以,我通常直接将它们设置为动态或静态资源。很抱歉,响应时间太长。就在我问了这个问题之后,我被拉上了另一个话题。谢谢你的回复。我会先尝试使用路径,然后使用其他建议(如果不起作用)