我试图获得"调暗整个窗口及其所有控件"的效果.
窗口及其上的所有内容也需要禁用.
问题是,当禁用按钮时,它似乎不允许您更改背景颜色.
有没有一种方法在WPF中更改按钮的背景颜色,即使它被禁用?
XAML:
<Window x:Class="TestDimWindows.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid x:Name="dimElement">
<StackPanel HorizontalAlignment="Left">
<TextBlock
Text="This is an example of dimming a window."
Margin="5"/>
<StackPanel
HorizontalAlignment="Left"
Margin="5">
<Button x:Name="theButton"
Content="Dim the window"
Click="Button_Click"/>
</StackPanel>
</StackPanel>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
代码背后:
using System.Windows;
using System.Windows.Media;
namespace TestDimWindows
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
dimElement.Background = new SolidColorBrush(Colors.Gray);
dimElement.Opacity = 0.5;
dimElement.IsEnabled = false;
//I want this button to look "dimmed out" as well
//but since it is disabled, it is a ghostly white.
//how can I change the color even though it is disabled?
theButton.Background = new SolidColorBrush(Colors.Gray);
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以为其创建自己的控件模板。
我建议使用 Blend(如果没有许可证,您可以获得试用版)来创建您当前使用的模板的副本。
如果您检查当前模板,它一定在某处将背景颜色设置为禁用。根据 IsEnabled 属性查找触发器。