Override a value in base style's resource
-
I have two style
FooStyle
andBarStyle
. I also have three TextBlocks and two of them use the styles above. In theFooStyle
I have specified adouble
(theWid
parameter) that is used to set the Width of the TextBlock (txt2) to 20. In theBarStyle
I would like to use theFooStyle
but override the Wid parameter with the value 40 on the TextBlock (txt3). But the txt3 TextBlock's Width don't get a longer Width. It still has the same Width as txt2. Here is my code<Style.Resources> <sys:Double x:Key="Wid">20</sys:Double> </Style.Resources> <Setter Property="Width" Value="{StaticResource Wid}" /> <Setter Property="FontWeight" Value="Bold" /> <Style.Resources> <sys:Double x:Key="Wid">40</sys:Double> </Style.Resources>
Is it possible to overwrite the Wid parameter in the BarStyle?
-
I have two style
FooStyle
andBarStyle
. I also have three TextBlocks and two of them use the styles above. In theFooStyle
I have specified adouble
(theWid
parameter) that is used to set the Width of the TextBlock (txt2) to 20. In theBarStyle
I would like to use theFooStyle
but override the Wid parameter with the value 40 on the TextBlock (txt3). But the txt3 TextBlock's Width don't get a longer Width. It still has the same Width as txt2. Here is my code<Style.Resources> <sys:Double x:Key="Wid">20</sys:Double> </Style.Resources> <Setter Property="Width" Value="{StaticResource Wid}" /> <Setter Property="FontWeight" Value="Bold" /> <Style.Resources> <sys:Double x:Key="Wid">40</sys:Double> </Style.Resources>
Is it possible to overwrite the Wid parameter in the BarStyle?
You've assigned the width using a
StaticResource
. If you want it to change based on the resources of the "derived" style, useDynamicResource
instead.<Setter Property="Width" Value="{DynamicResource Wid}" />
You can then override the value with the resources of a "derived" style:
<Style x:Key="BarStyle" TargetType="TextBlock" BasedOn="{StaticResource FooStyle}">
<Style.Resources>
<sys:Double x:Key="Wid">40</sys:Double>
</Style.Resources>
</Style>Or directly on the control:
<TextBox Text="txt4" Background="Purple" Style="{StaticResource FooStyle}">
<Style.Resources>
<sys:Double x:Key="Wid">80</sys:Double>
</Style.Resources>
</TextBox>
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
You've assigned the width using a
StaticResource
. If you want it to change based on the resources of the "derived" style, useDynamicResource
instead.<Setter Property="Width" Value="{DynamicResource Wid}" />
You can then override the value with the resources of a "derived" style:
<Style x:Key="BarStyle" TargetType="TextBlock" BasedOn="{StaticResource FooStyle}">
<Style.Resources>
<sys:Double x:Key="Wid">40</sys:Double>
</Style.Resources>
</Style>Or directly on the control:
<TextBox Text="txt4" Background="Purple" Style="{StaticResource FooStyle}">
<Style.Resources>
<sys:Double x:Key="Wid">80</sys:Double>
</Style.Resources>
</TextBox>
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer