Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Override a value in base style's resource

Override a value in base style's resource

Scheduled Pinned Locked Moved WPF
wpfquestionlearning
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mc_Topaz
    wrote on last edited by
    #1

    I have two style FooStyle and BarStyle. I also have three TextBlocks and two of them use the styles above. In the FooStyle I have specified a double (the Wid parameter) that is used to set the Width of the TextBlock (txt2) to 20. In the BarStyle I would like to use the FooStyle 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?

    R 1 Reply Last reply
    0
    • M Mc_Topaz

      I have two style FooStyle and BarStyle. I also have three TextBlocks and two of them use the styles above. In the FooStyle I have specified a double (the Wid parameter) that is used to set the Width of the TextBlock (txt2) to 20. In the BarStyle I would like to use the FooStyle 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?

      R Offline
      R Offline
      Richard Deeming
      wrote on last edited by
      #2

      You've assigned the width using a StaticResource. If you want it to change based on the resources of the "derived" style, use DynamicResource 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

      M 1 Reply Last reply
      0
      • R Richard Deeming

        You've assigned the width using a StaticResource. If you want it to change based on the resources of the "derived" style, use DynamicResource 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

        M Offline
        M Offline
        Mc_Topaz
        wrote on last edited by
        #3

        Thank you! I got it to work in my example application. But in my real application fail hard. I'll make a new post about it.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups