views:

256

answers:

2

I am modifying the control template of the WPF Calendar control to change the color of text and the Previous and Next arrows on the control. I want to set the color to a local SolidColorBrush resource called MyTextBrush.

The Previous and Next buttons have separate control templates, and each draws a Path object for its button's arrow. Here is the relevant markup for the Previous button:

<Path Margin="14,-6,0,0" Height="10" Width="6" VerticalAlignment="Center" HorizontalAlignment="Left" Stretch="Fill" Data="M288.75,232.25 L288.75,240.625 L283,236.625 z">
    <Path.Fill>
        <SolidColorBrush x:Name="TextColor" Color="#FF333333" />
    </Path.Fill>
</Path>

Note that the color is named TextColor, using the x:Name property.

Here is my problem: The x:Name property is required--WPF throws an exception if it is missing. That means I can't simply replace the entire brush with a reference to the MyTextBrush resource, because I would lose the x:Name value. So, how do I reference MyTextBrush, while still retaining the x:Name property for the brush in this particular control template?

Thanks for your help.

+1  A: 

So, how do I reference MyTextBrush, while still retaining the x:Name property for the brush in this particular control template?

Regarding this problem it sounds like you are using a dodgy/fragile template. What control template is it?

  • If you have full source control of the template, remove references to the named element (most likely in a storyboard). They must be animating the brush for some reason.
  • The other option might be to just create another unused brush within your template (Perhaps on a hidden element) with the correct name to keep the template happy.
  • Lastly, you can try adding the x:Name onto the brush in the shared RD, but this is quite complicated and not sure its worth it!

Two more potential solutions:

  • Try binding just the Color property of the SCB... that should work as its a DP
  • Change the template animations so they do not use a named brush, but instead use a named parent then access the brush via the TargetProperty e.g. Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="myNamedParent"
Schneider
Dodgy doesn't begin to describe it. It's the control template for the WPF Calendar control, and it is a reall mess. It's requiring major surgery just to bind the calendar text color to the control's Foreground property.
David Veeneman
Do you wish to preserve the Calendar text color animations? If not just rip out the storyboards
Schneider
A: 

The best solution seems to be to break the Color property out to its own tag, and use a resource reference for that. Here is what it looks like:

<!-- FS: Changed template brush color -->
<SolidColorBrush x:Name="TextColor">
    <SolidColorBrush.Color>
        <StaticResource ResourceKey="FsTextColor" />
    </SolidColorBrush.Color>
</SolidColorBrush>
David Veeneman