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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. WCF and WF
  4. Trigger ignored when style added programically

Trigger ignored when style added programically

Scheduled Pinned Locked Moved WCF and WF
wpfdatabasequestion
2 Posts 1 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
    MBursill
    wrote on last edited by
    #1

    I have two simple styles. One with a trigger, setting the background of a TextBox when the IsReadOnly style is true:

    <Style TargetType="{x:Type TextBox}">
    <Style.Triggers>
    <Trigger Property="IsReadOnly" Value="True">
    <Setter Property="Background" Value="{StaticResource TextBoxReadOnlyBackgroundBrush}" />
    </Trigger>
    </Style.Triggers>
    </Style>

    The second style is setting the IsReadOnly property to true:

    <Style TargetType="{x:Type TextBox}">
    <Setter Property="IsReadOnly" Value="True" />
    </Style>

    I add the second style dynamically, like this:

    public void AddReadOnlyStyles()
    {
    ResourceDictionary readOnlyStyles = new ResourceDictionary();
    readOnlyStyles.Source = new Uri("ReadOnlyStyles.xaml");

    if (Resources.MergedDictionaries.Contains(readOnlyStyles) == false)
        Resources.MergedDictionaries.Add(readOnlyStyles);
    

    }

    The read only style is added just fine, but the trigger is ignored. Why is it being ignored? Is there a way to make it so it wont be ignored? Thanks -Mike.

    M 1 Reply Last reply
    0
    • M MBursill

      I have two simple styles. One with a trigger, setting the background of a TextBox when the IsReadOnly style is true:

      <Style TargetType="{x:Type TextBox}">
      <Style.Triggers>
      <Trigger Property="IsReadOnly" Value="True">
      <Setter Property="Background" Value="{StaticResource TextBoxReadOnlyBackgroundBrush}" />
      </Trigger>
      </Style.Triggers>
      </Style>

      The second style is setting the IsReadOnly property to true:

      <Style TargetType="{x:Type TextBox}">
      <Setter Property="IsReadOnly" Value="True" />
      </Style>

      I add the second style dynamically, like this:

      public void AddReadOnlyStyles()
      {
      ResourceDictionary readOnlyStyles = new ResourceDictionary();
      readOnlyStyles.Source = new Uri("ReadOnlyStyles.xaml");

      if (Resources.MergedDictionaries.Contains(readOnlyStyles) == false)
          Resources.MergedDictionaries.Add(readOnlyStyles);
      

      }

      The read only style is added just fine, but the trigger is ignored. Why is it being ignored? Is there a way to make it so it wont be ignored? Thanks -Mike.

      M Offline
      M Offline
      MBursill
      wrote on last edited by
      #2

      I managed to solve this. By giving the first style (the one with the trigger) a key, calling it TextBoxStyle, and adding BasedOn="{StaticResource TextBoxStyle}" to the second style, it works. Don't fully understand why though. -Mike.

      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