locked
change property of style programatically RRS feed

  • Question

  • How can you change a value of a property of a style programatically?

    e.g. the value of Source in the Image below

    1        <Style x:Key="ImageButtonFourOn"
    2               TargetType="Button">
    3            <Setter Property="Template">
    4                <Setter.Value>
    5                    <ControlTemplate TargetType="Button">
    6                        <Image  x:Name="TheImage"
    7                                Source="/xxx;component/Images/button-Four-on.png" />
    8                    </ControlTemplate>
    9                </Setter.Value>
    10           </Setter>
    11       </Style>
    12   
    
     
    Thursday, June 3, 2010 8:36 AM

Answers

  • This what I was trying to ask for:

     

     ButtonOne.Style = Application.Current.Resources.MergedDictionaries[0]["ImageButtonOneOn"] as Style;
                ButtonTwo.Style = Application.Current.Resources.MergedDictionaries[0]["ImageButtonTwoOff"] as Style;
                ButtonThree.Style = Application.Current.Resources.MergedDictionaries[0]["ImageButtonThreeOff"] as Style;
                ButtonFour.Style = Application.Current.Resources.MergedDictionaries[0]["ImageButtonFourOff"] as Style;
     
    Thursday, June 3, 2010 9:32 AM

All replies

  • var s = new Style(typeof(TextBlock));

    s.Setters.Add(new Setter(TextBlock.FontSizeProperty, 45));

    // ... Add as many setters as you want

    txtBlock.Style = s;

    Thursday, June 3, 2010 8:55 AM
  • This what I was trying to ask for:

     

     ButtonOne.Style = Application.Current.Resources.MergedDictionaries[0]["ImageButtonOneOn"] as Style;
                ButtonTwo.Style = Application.Current.Resources.MergedDictionaries[0]["ImageButtonTwoOff"] as Style;
                ButtonThree.Style = Application.Current.Resources.MergedDictionaries[0]["ImageButtonThreeOff"] as Style;
                ButtonFour.Style = Application.Current.Resources.MergedDictionaries[0]["ImageButtonFourOff"] as Style;
     
    Thursday, June 3, 2010 9:32 AM