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:
<LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#CCC" Offset="1.0"/>
<!-- SimpleStyles: Button -->
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="MinHeight" Value="23"/>
<Setter Property="MinWidth" Value="75"/>
<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"/>
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
b.Template = MethodGetsControlTemplateFromString(myxaml2);
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!
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?
- Edited by paarthtoraskar Monday, March 26, 2012 8:26 AM