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:
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.