Button Foreground not changing when content is Textblock

Pratham Jain 281 Reputation points
2023-11-18T07:04:40.56+00:00

Hi All,

I have a button in XAML with content Textblock control. I am trying to change the foreground and background of button when it is disabled inside trigger. The background of button is getting changed by trigger when it is disabled but not foreground. Please advise how to change the foreground ASAP.

Regards,

Pratham

Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,681 Reputation points Microsoft External Staff
    2023-11-20T03:20:12.88+00:00

    Hi,@Pratham Jain. Welcome to Microsoft Q&A Forum.

    Dependency property value precedence is set in wpf. Dependency property value precedence exists so that the various scenarios for how properties obtain their values interact in a predictable way. For more information you can refer to Dependency property value precedence (WPF .NET), which contains the Dependency property precedence list.

    As gekka said, it should make the text's foreground inherit from the button. You could remove the code Foreground="White" from the TextBlock.

    Before updating the code:

    
    <TextBlock Text="Add" Foreground="White"/>
    
    

    After updating the code:

    
    <TextBlock Text="Add" />
    
    
     <Window.Resources>
             <Style TargetType="Button">
                <Setter Property="Background" Value="SlateBlue"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border Background="{TemplateBinding Background}" BorderBrush="Transparent">
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Background" Value="SlateGray"></Setter>
                    <Setter Property="TextElement.Foreground" Value="Black"></Setter>
                </Trigger>
                </Style.Triggers>
            </Style>
        </Window.Resources>
        <StackPanel>
            <Button Name="btn1" HorizontalAlignment="Left" Click="Button_Click">
                    <TextBlock Text="Add"  />
            </Button>
    </StackPanel>
    
    
    ------
    
     private void Button_Click(object sender, RoutedEventArgs e)
        {
          if(btn1.IsEnabled=false) 
          { btn1.IsEnabled = true; }
          else  if(btn1.IsEnabled = true)
          {
            btn1.IsEnabled = false;
          }
       
    
         }
    
    
    

    The result:

    8


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. gekka 12,206 Reputation points MVP Volunteer Moderator
    2023-11-18T10:46:31.89+00:00

    If you are hurry, you should present all your XAML.

    If not set color directly to the Text block, the foreground color of the button is inherit to the Textblock.

    <StackPanel HorizontalAlignment="Left" >
        <StackPanel.Resources>
                  
            <Style x:Key="buttonStyle" TargetType="{x:Type Button}">
                <Setter Property="FontSize" Value="20" />
                <Setter Property="Margin" Value="5" />
                <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="True">
                        <Setter Property="Background" Value="Orange" />
                        <Setter Property="Foreground" Value="Black" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Background" Value="LightBlue" />
                        <Setter Property="TextElement.Foreground" Value="Gray" />
                    </Trigger>
                </Style.Triggers>
            </Style>
                  
        </StackPanel.Resources>
                
        <Button IsEnabled="{Binding ElementName=chk,Path=IsChecked}"
                Style="{StaticResource buttonStyle}">
            <TextBlock Text="Not Good" Foreground="Red" />                 
        </Button>
    
        <Button IsEnabled="{Binding ElementName=chk,Path=IsChecked}"
                Style="{StaticResource buttonStyle}">
                    
            <TextBlock Text="Good" />
        </Button>
    
        <CheckBox x:Name="chk" Margin="5" FontSize="20" Content="Enable" />
    
    </StackPanel>
    
    0 comments No comments

  2. Pratham Jain 281 Reputation points
    2023-11-18T14:23:36.7733333+00:00

    Hi @gekka ,

    Many thanks for your reply. I have tried your above solution but it didn't work for me. Below is the code:

    <Button Name="btn1" HorizontalAlignment="Left">     
    <Button.Content>         
    <TextBlock Text="Add" Foreground="White"/>     
    </Button.Content>
    </Button>
    
    <Style TargetType="Button">     
    <Setter Property="Background" Value="SlateBlue"/>     
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Template">     
    <Setter.Value>         
    <ControlTemplate TargetType="{x:Type Button}">             
    <Border Background="{TemplateBinding Background}" BorderBrush="Transparent">                 <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>             </Border>
    </ControlTemplate>     
    </Setter.Value> 
    </Setter>
    <Trigger Property="IsEnabled" Value="False">          
    <Setter Property="Background" Value="SlateGray"></Setter>          
    <Setter Property="TextElement.Foreground" Value="Black"></Setter>      
    </Trigger>  
    </Style.Triggers>
    </Style>
    

    Background of button is changed to "SlateGray" by Trigger when button is disabled but Foreground is not changed to "Black". Please suggest how to fix the same ASAP.

    Regards,

    Pratham


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.