Joe
Joe

Reputation: 6826

Difficulties binding colors for WPF ComboBoxItem ItemTemplate with TemplateBinding

I'm trying to implement a simple combo box (bound to a list of strings) that shows each item as the string label on the right and icon (which is in the form of a GeometryDrawing) on the left. I want the items in the box to inherit the colors I'm using because I've got a custom scheme (with a dark background and white text).

I am able to use TemplateBinding (binding to to ComboBoxItem.Foreground) to make the colors of the text label appear properly but am not able to do the same for the Geometry drawing and I can't figure out why.

Here an image of what I am trying to achieve.

Icon shows up

Note that the icon shows up nicely to the left of the name. This only works if I explicitly specify "White" as the brush of the GeometryDrawing.

Below is the XAML I'm using to do make this work.

<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>

A couple of notes:

Unfortunately I am NOT a able to use that same TemplateBinding for the GeometryDrawing's "Brush" property. When I try...

<GeometryDrawing Brush="{TemplateBinding tk:RadComboBoxItem.Foreground}"
                 Geometry="{Binding Converter={StaticResource PathGeometryConverter}}"
/>

I end up with this:

Icon invisible

Therefore I am forced to explicitly specify "White" as the brush color for the Geometry drawing to make this work.

What is the proper way to make my GeometryDrawing in my template "inherit" the surrounding color scheme...? Is there some sort of TemplateBinding I am missing?

Upvotes: 0

Views: 60

Answers (1)

Andy
Andy

Reputation: 12276

I think your problem is because you're using templatebinding combined with something that is coming out of a resource and or because it's several levels down from the thing being templated. If the geometry was directly in the template then I think it'd work.

What I suggest is you instead try a binding with relativesource and ancestortype tk:RadComboBoxItem, path=foreground. Or relativesource templatedparent.

I would also use a path rather than an image. It might make very little difference in this case but paths can be sharper. You can bind the Data of a Path to a Geometry resource. Or at least I think you can, I usually set them to a dynamic or staticresource directly.

Upvotes: 1

Related Questions