Using {TemplateBinding Content} withTextBlock in ControlTemplate

2.2k views Asked by At

Basically I have a button that I built a control template for. It is currently working but VS 2010 complains about the following line of code in my control template

<TextBlock Text="{TemplateBinding Content}" 
    Foreground="{TemplateBinding Foreground}" 
    x:Name="cContentTextBlock"  />

The control template is for a Button and I have a variety of VisualStates that target this TextBlock.

I can see why VS2010 complains... What if the content isn't actually text? This would cause problems. For me the big deal is that I want to set the Foreground of the text in response to Visual State changes.

Any ideas on how I could accomplish this? (try it, it works... but the vs2010 designer chokes on it)

The following is the entire style:

<Style x:Key="PassiveLinkButton" TargetType="Button">
    <Setter Property="Cursor" Value="Hand" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Grid>
                    <vsm:VisualStateManager.VisualStateGroups>
                        <vsm:VisualStateGroup x:Name="CommonStates">
                            <vsm:VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="cMouseOverBorder" Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <SolidColorBrush Color="Black" />
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </vsm:VisualState>
                            <vsm:VisualState x:Name="Normal"/>
                            <vsm:VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="cMouseOverBorder" Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <SolidColorBrush Color="Blue" />
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="cContentTextBlock" Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <SolidColorBrush Color="Blue" />
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </vsm:VisualState>
                            <vsm:VisualState x:Name="Disabled"/>
                        </vsm:VisualStateGroup>
                    </vsm:VisualStateManager.VisualStateGroups>
                    <Border x:Name="cFocusBorder" 
                                BorderThickness="1" 
                                BorderBrush="{StaticResource BorderBrush}"
                                Margin="2">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Border x:Name="cMouseOverBorder" 
                                BorderBrush="Transparent" BorderThickness="0,0,0,1" Margin="0 0 0 2">
                                <StackPanel HorizontalAlignment="Left">
                                    <TextBlock Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" 
                                                x:Name="cContentTextBlock" Margin="2 2 2 0"
                                                HorizontalAlignment="Center" />
                                </StackPanel>
                            </Border>
                        </Grid>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>

    </Setter>
</Style>

Which would be used like:

<Button Content="Press Me" Style={StaticResource PassiveButtonLink}" />
2

There are 2 answers

0
Aaron Hoffman On

I would read through this MSDN documentation if you haven't already:

Walkthrough: Customizing the Appearance of a Button by Using a ControlTemplate http://msdn.microsoft.com/en-us/library/cc903963(VS.95).aspx

and if you need to go deeper than that:

Customizing the Appearance of an Existing Control by Using a ControlTemplate http://msdn.microsoft.com/en-us/library/cc189093(VS.95).aspx

Creating a New Control by Creating a ControlTemplate
http://msdn.microsoft.com/en-us/library/cc278064(VS.95).aspx

0
Wallace Kelly On

I had a similar problem. The solution for me was to change my default XML namespace.

The following works in my copy of VS2008, but not in VS2010:

xmlns="http://schemas.microsoft.com/netfx/2007/xaml/presentation"

The following works in both VS2008 and VS2010:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

By "works", I mean that the WPF designer recognizes the TemplateBinding element.