FindResource style not applied correctly

I am creating a button within C# and applying a style to it via FindResource. This method works fine and the template is applied to the button, but after the style is applied to the button created in code, the template beneath the style (button appearance) appears to work fine but none of the properties of the style (fontsize, foreground, etc) display.

Here's a sample of the control creation:

Code Snippet

Button b = new Button();

b.Name = "Button";

RegisterName(b.Name, b);

b.Style = (Style)FindResource("FieldControlButton");

And a sample of the applied style:

Code Snippet

<Style x:Key="FieldControlButton" TargetType="{x:Type Button}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Border Opacity="0" Background="#FF000000" />
<Border Style="{DynamicResource ControlBorderInner}" x:Name="border" Opacity="0" Margin="{DynamicResource ControlBorderThickness}"/>
<Border x:Name="border2" BorderBrush="#FFCCCCCC" BorderThickness="0,0,0,1">
<ContentPresenter x:Name="contentPresenter"/>
</Border>
</Grid>

<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Style" TargetName="border" Value="{DynamicResource ControlBorderInnerDisabled}"/>
<Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}"/>
</Trigger>

</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="{DynamicResource NormalForegroundBrushAlt}"/>
<Setter Property="FontSize" Value="{DynamicResource FontSizeDefault}"/>
</Style>

Any help would be appreciated. Thanks.
[2787 byte] By [hightouch] at [2008-1-8]
# 1

Hi there,

since you are creating a control template the button will loose all its UI lokks (font size etc)

what you need to do in order to use the FontSize etc .. in your button is to include them in you control template. You can do this by using {TemplateBinding FontSize} in your control template

example..

<TextBlock FontSize={TemplateBinding FontSize}" />

Hope this helps

MarlonGrech at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2

I'm trying to apply this to a ContentPresenter which doesn't recognize FontSize. Is there a way around this?

hightouch at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 3

Hello, this works fine for me even though I’ve set a ControlTemplate:

<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">

<Setter Property="FontSize" Value="48"/>

<Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>

<Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>

<Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>

<Setter Property="BorderThickness" Value="1"/>

<Setter Property="Foreground" Value="{DynamicResource {xTongue Tiedtatic SystemColors.ControlTextBrushKey}}"/>

<Setter Property="HorizontalContentAlignment" Value="Center"/>

<Setter Property="VerticalContentAlignment" Value="Center"/>

<Setter Property="Padding" Value="1"/>

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="{x:Type Button}">

<Border BorderBrush="#FF000000" BorderThickness="1,1,1,1">

<ContentPresenter/>

</Border>

<ControlTemplate.Triggers>

<Trigger Property="IsKeyboardFocused" Value="true"/>

<Trigger Property="ToggleButton.IsChecked" Value="true"/>

<Trigger Property="IsEnabled" Value="false">

<Setter Property="Foreground" Value="#ADADAD"/>

</Trigger>

</ControlTemplate.Triggers>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

Yi-LunLuo-MSFT at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 4

The problem ended up being that I was setting the control's "IsEnabled" property to false on creation of the button in code before setting the style, then later changing it to true if it met a certain condition. I then changed the functionality to have the button enabled until it failed a certain condition then was disabled. Since then I changed around a bunch of my logic and the style now sets correctly, but unfortunately I can no longer reproduce the original problem to be entirely sure what was causing it in the first place..

hightouch at 2007-10-2 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified