Home > Software engineering >  WPF: How to assign IsMouseOver and IsPressed Button State backgrounds from Code Behind no XAML
WPF: How to assign IsMouseOver and IsPressed Button State backgrounds from Code Behind no XAML

Time:08-01

I have some runtime generated WPF buttons that I need to style using code behind. I have the Style below in a code behind variable ( Style Editstyle = Application.Current.Resources[x] as Style;)

How would I change the DynamicResource of the values in the Trigger section from codebehind? Like style.Trigger.IsFocused.Background = bitmapX; Also style.Key="NewStyleCopy1";

    <Style x:Key="INIT_A" TargetType="Button">
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="Background" Value="{DynamicResource back}" />
        <Setter Property="Foreground" Value="{DynamicResource fore}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Name="border"
                                                BorderBrush="{DynamicResource stroke}"
                                                BorderThickness="1"
                                                Padding="4,2"
                                                CornerRadius="0"
                                                Background="{TemplateBinding Background}">
                        <Grid>
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="content" />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="Background" Value="{DynamicResource altback}" />
                            <Setter Property="Foreground" Value="{DynamicResource altfore}" />
                            <Setter TargetName="border" Property="BorderBrush" Value="{DynamicResource altstroke}" />
                            <Setter Property="Button.Effect" Value="{DynamicResource a_hbtnglow}" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="{DynamicResource altback}" />
                            <Setter Property="Foreground" Value="{DynamicResource altfore}" />
                            <Setter TargetName="border" Property="BorderBrush" Value="{DynamicResource altstroke}" />
                            <Setter Property="Button.Effect" Value="{DynamicResource a_hbtnglow}" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" Value="{DynamicResource altback}" />
                            <Setter Property="Foreground" Value="{DynamicResource altfore}" />
                            <Setter TargetName="border" Property="BorderBrush" Value="{DynamicResource altstroke}" />
                            <Setter Property="Button.Effect" Value="{DynamicResource a_hbtnglow}" />
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

CodePudding user response:

This is usually done in XAML by defining a Trigger for each state:

<Style TargetType="Button">
  <Style.Triggers>
    <Trigger Property="IsPressed" Value="True">
      <Setter Property="Foreground" Value="Green" />
    </Trigger>
  </Style.Triggers>
</Style>

You can make use of the Style.BasedOn property to extend a particular Style, for example a default Style for all Button elements. This way you can create a more specific Style that includes all attributes of the base Style:

<Style x:Key="DefaultButtonStyle" TargetType="Button">
  <Setter Property="Background" Value="Orange" />
</Style>

<Style x:Key="GeneratedButtonsStyle" 
       BasedOn="{StaticResource DefaultButtonStyle}"
       TargetType="Button">

  <!-- Override the base Style value -->
  <Setter Property="Background" Value="Yellow" />

  <!-- Extend the base Style -->
  <Setter Property="Foreground" Value="Red" />

  <Style.Triggers>
    <Trigger Property="IsPressed" Value="True">
      <Setter Property="Foreground" Value="Green"/>
    </Trigger>
  </Style.Triggers>
</Style>

CodePudding user response:

I ended up copying the style and edited the properties. I appreciate all your suggestions.

            var xu = styleCopy("ButtonAA");
            Trigger t = new Trigger();
            t.Property = Button.IsMouseOverProperty;
            t.Value = true;
            Setter setter = new Setter();
            setter.Property = Button.BackgroundProperty;
            setter.Value = Brushes.Blue;
            t.Setters.Add(setter);
            xu.Triggers.Add(t);
            btn.Style = xu;
  • Related