none
How to change a ControlTemplate a runtime?

    Question

  • Hi there,

     

    is it possible to change a property of a ControlTemplate dynamically by code at runtime?

    I defined a style for the Button control which contains a ControlTemplate.

    Is it possible to change a property within ControlTemplate (e.g BorderBrush) so that the change afterwards is visible in every button instance within my window?

    I don't want to create a new style at runtime, everything i want to do is, to alter an already existing style which is used by all button instances.

     

    This is my sample xaml:

     

    <Window>

     

    <Window.Resources>

     

    <LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">

    <GradientBrush.GradientStops>

    <GradientStopCollection>

    <GradientStop Color="#FFF" Offset="0.0"/>

    <GradientStop Color="#CCC" Offset="1.0"/>

    </GradientStopCollection>

    </GradientBrush.GradientStops>

    </LinearGradientBrush>

     

    <!-- SimpleStyles: Button -->

    <Style TargetType="Button">

    <Setter Property="SnapsToDevicePixels" Value="true"/>

    <Setter Property="OverridesDefaultStyle" Value="true"/>

    <Setter Property="MinHeight" Value="23"/>

    <Setter Property="MinWidth" Value="75"/>

    <Setter Property="Template">

    <Setter.Value>

    <ControlTemplate TargetType="Button">

    <Border

    x:Name="Border"

    CornerRadius="2"

    BorderThickness="5"

    Background="{StaticResource NormalBrush}"

    BorderBrush="Green">

    <ContentPresenter

    Margin="2"

    HorizontalAlignment="Center"

    VerticalAlignment="Center"

    RecognizesAccessKey="True"/>

    </Border>

    </ControlTemplate>

    </Setter.Value>

    </Setter>

    </Style>

    </Window.Resources>

     

     

    <Grid>

    <Grid.ColumnDefinitions>

    <ColumnDefinition/>

    <ColumnDefinition/>

    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>

    <RowDefinition/>

    <RowDefinition/>

    </Grid.RowDefinitions>

     

    <Button x:Name="Button1" Grid.Column="0" Grid.Row="0" Content="Button1" Margin="10"/>

    <Button x:Name="Button2" Grid.Column="1" Grid.Row="0" Content="BTN" Margin="10"/>

    </Grid>

    </Window>

     

    Any ideas?

    Friday, August 24, 2007 2:24 PM

Answers

  • Instead of using a StaticResource to reference the Brush used to render the Border, use a DynamicResource.  When you remove the old "NormalBrush" and replace it with a new one at runtime, that new Brush will be used instead.

     

    Friday, August 24, 2007 3:57 PM

All replies

  • Instead of using a StaticResource to reference the Brush used to render the Border, use a DynamicResource.  When you remove the old "NormalBrush" and replace it with a new one at runtime, that new Brush will be used instead.

     

    Friday, August 24, 2007 3:57 PM
  • May you know how to do this in code (C#) ?

    I have xaml control templates in strings and i would load them in specific cases.

    Let say i have sth like this:

    ControlTemplate MethodGetsControlTemplateFromString(string xaml);
    ...
    Button b = new Button();
    b.Template = MethodGetsControlTemplateFromString(myxaml1); //it works fine
    ...
    //different place in code
    if(var==true)
    {
          b.Template = MethodGetsControlTemplateFromString(myxaml2);
    }
    else
    {
          b.Template = MethodGetsControlTemplateFromString(myxaml3);
    }

    Unfortunately this changing at runtime doesn't work..

    Do you have any idea how to achieve my goal?

    Thanks in advance!
    Majkel
    Thursday, November 22, 2007 11:43 AM
  • Ok, solved.

    The problem was somewhere else.

    Cheers
    Majkel
    Thursday, November 22, 2007 12:13 PM
  • I have a similar issue. However, changing from Static to DynamicResource is not working.

    I have a style for a ToolTip. I am setting the 'Template' property on the ToolTip in the style. In the ControlTemplate, I have an object of Path, the Data property of which I have set to a DynamicResource (using the logic mentioned above).

    I have defined 2 PathGeometry objects as resources.

    On the ToolTip.Loaded event, in the code-behind, I am assigning the second modified PathGeometry to the first. Although in the debugger the Path.Data is seen to be changed, the change is not reflected in the GUI!

    Would you happen to know about this?


    Monday, March 26, 2012 8:17 AM