Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For a Border within a Template specified under a Style:
HTML
<Thickness x:Key="CommonBorderThickness">2</Thickness>
<Style x:Key="CommonButtonStyle" TargetType="Button">
...
   <Setter Property="BorderThickness" Value="2" />
...
   <Setter Property="Template">
               <Setter.Value>
                   <ControlTemplate TargetType="Button">
...
                         <Border Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" Grid.RowSpan="2"

this works fine:
HTML
BorderThickness="{Binding Source={StaticResource CommonBorderThickness}, Converter={StaticResource FourGroupSizeConverter}}"

but these do not:
HTML
BorderThickness="{TemplateBinding BorderThickness, Converter={StaticResource FourGroupSizeConverter}}"
BorderThickness="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness, Converter={StaticResource FourGroupSizeConverter}}" 

Any ideas as to why?

Update - Here's the full Template FYI...

HTML
<ControlTemplate x:Name="CommonButtonControlTemplate" TargetType="Button">
    <Grid>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal" />
                <VisualState x:Name="Disabled">
                    <Storyboard>
                        <ColorAnimation Duration="0" Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="Color" To="DimGray" />
                        <ColorAnimation Duration="0" Storyboard.TargetName="ButtonBorderBrush" Storyboard.TargetProperty="Color" To="Gray" />
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="MouseOver">
                    <Storyboard>
                        <ColorAnimation Duration="0" Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="Color" To="Red" />
                        <ColorAnimation Duration="0" Storyboard.TargetName="ButtonBorderBrush" Storyboard.TargetProperty="Color" To="Lime" />
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="Pressed">
                    <Storyboard>
                        <ColorAnimation Duration="0" Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="Color" To="Red" />
                        <ColorAnimation Duration="0" Storyboard.TargetName="ButtonBorderBrush" Storyboard.TargetProperty="Color" To="Lime" />
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
            <VisualStateGroup x:Name="FocusStates">
                <VisualState x:Name="Focused">
                    <Storyboard>
                        <ColorAnimation Duration="0" Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="Color" To="Red" />
                        <ColorAnimation Duration="0" Storyboard.TargetName="ButtonBorderBrush" Storyboard.TargetProperty="Color" To="Lime" />
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="Unfocused" />
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="20*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="18*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Border Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" Grid.RowSpan="2" BorderBrush="Black" removed="Black" Opacity="0.3"
                                BorderThickness="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness, Converter={StaticResource FourGroupSizeConverter},
                                FallbackValue={Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness}}"
                                CornerRadius="{Binding Source={StaticResource CornerRadius}, Converter={StaticResource FourGroupSizeConverter},
                                FallbackValue={StaticResource CornerRadius}}" />
        <Border Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" Grid.RowSpan="2"
                                BorderThickness="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness, Converter={StaticResource FourGroupSizeConverter},
                                FallbackValue={Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness}}"
                                CornerRadius="{Binding Source={StaticResource CornerRadius}, Converter={StaticResource FourGroupSizeConverter},
                                FallbackValue={StaticResource CornerRadius}}">
            <Border.Background>
                <SolidColorBrush x:Name="ButtonBackground" Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}"/>
            </Border.Background>
            <Border.BorderBrush>
                <SolidColorBrush x:Name="ButtonBorderBrush" Color="{TemplateBinding BorderBrush}"/>
            </Border.BorderBrush>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="14*" />
                    <ColumnDefinition Width="14*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="10*" />
                    <RowDefinition Height="10*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Image Grid.Column="1" Grid.Row="1" Source="/Assets/en-ie/Common/pawprint.png" Margin="0,8,8,0" />
                <ContentPresenter Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2">
                    <TextBlock Text="{TemplateBinding Content}" TextAlignment="Center"
                                    FontSize="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=FontSize, Converter={StaticResource SizeConverter},
                                    FallbackValue={Binding RelativeSource={RelativeSource TemplatedParent}, Path=FontSize}}" />
                </ContentPresenter>
            </Grid>
        </Border>
    </Grid>
</ControlTemplate>
Posted
Updated 14-Dec-15 17:39pm
v3
Comments
Richard Deeming 14-Dec-15 12:34pm    
Using {TemplateBinding BorderThickness} (without the converter) should work fine.

Why do you need the converter?
FrontrunnerSoftwareIreland 14-Dec-15 22:41pm    
Perhaps it would, but that's not what is needed as the converter is required. The converter adapts the thickness based on the window size. In any case, I've already tried it with the converter removed and it makes no difference... the strange thing is, both the TemplateBinding and Binding RelativeSource structures work fine on a line further down the Template on the TextBlock for the Content and Fontsize values and the Fontsize also uses a Converter.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900