wpf - 为什么为控件分配背景颜色会覆盖样式触发器?

标签 wpf

我有一个带有标签的简单 WPF 窗口。窗口中有一个默认样式,当鼠标悬停在标签上时,会将标签背景更改为红色。很简单。

只要我不将标签颜色设置/修改为任何地方的任何内容,这就有效。

  • 如果我在后面的代码中设置标签背景(通过下面代码中的“设置标签背景”按钮),样式触发器将停止工作
  • 如果我在 XAML 中设置标签背景(未显示,但易于编辑),则样式不起作用。
  • 如果在后面的代码中设置标签颜色后,我尝试将背景恢复到原来的状态(无论是空还是透明),触发器就会从工作变为不工作。

我不明白为什么设置背景似乎会覆盖样式。

我希望这样做是保持样式处于事件状态,无论我为标签设置什么背景 - 如果我将鼠标移到上方,它应该将颜色更改为红色,无论我是否将其设置为蓝色或其他任何东西。如果我必须在后面的代码中创建触发器,我没问题。

相反,似乎仅仅通过为标签分配背景颜色,我就覆盖了样式。

这是 XAML:

    <Grid Background="Black">   
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Button Grid.Column="0" Grid.Row="0" Margin="10,10,10,10" Height="75" Click="Button_Set_Click">Set Label Background</Button>
    <Button Grid.Column="0" Grid.Row="1" Margin="10,10,10,10" Height="75" Click="Button_Clear_Click">Clear Label Background</Button>
    <Label Name="TestLabel" Grid.Row="2"  Margin="10,10,10,10" BorderBrush="Gray" BorderThickness="5"></Label>
    </Grid>

这是背后的代码:

public partial class WpfProblems : Window
{
    public WpfProblems()
    {
        InitializeComponent();
    }

    private void Button_Clear_Click(object sender, RoutedEventArgs e)
    {
        // revert the label background color - I've tried setting to null, transparent also
        // but the trigger never comes back. 
        TestLabel.Background = (Brush)new BrushConverter().ConvertFrom("#00FFFFFF");            
    }

    private void Button_Set_Click(object sender, RoutedEventArgs e)
    {
        // Set the label background color
        TestLabel.Background = Brushes.Blue;
    }
}

最佳答案

通过为标签分配颜色,您将覆盖其 Color 属性(之前保存了绑定(bind))。因此,最终您将通过直接在代码后面或 xaml 中分配颜色来破坏绑定(bind)。

要解决此问题,您必须在标签样式中指定默认颜色,以便在触发条件完成后保留该颜色。

假设您的风格如下所示。请注意我如何为触发器部分上方的标签设置默认颜色。

<Style TargetType="{x:Type Label}">
<Setter Property="Background" Value="Blue"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="Red/>
</Trigger>
</Style.Triggers>
</Style>

编辑-

由于您使用的是代码隐藏文件,我认为您可以使用 DependencyProperty 来控制标签的颜色。我在测试应用程序上尝试过这个,它有效。

创建一个 dependencyProperty,如下所示。请注意,与测试应用程序中一样,默认颜色为蓝色。

public Brush BackgroundColor
        {
            get { return (Brush)GetValue(BackgroundColorProperty); }
            set { SetValue(BackgroundColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BackgroundColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BackgroundColorProperty =
            DependencyProperty.Register("BackgroundColor", typeof(Brush), typeof(MainWindow), new PropertyMetadata(Brushes.Blue));

现在你的按钮点击事件将变成

private void Button_Click(object sender, RoutedEventArgs e)
        {
            BackgroundColor = Brushes.Yellow;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            BackgroundColor = Brushes.Blue;
        }

在主窗口(或您的用户控件)中,我为标签定义了以下样式

<Window.Resources>
        <Style TargetType="{x:Type Label}">
            <Setter Property="Background" Value="{Binding BackgroundColor}"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>



<Grid Background="Black">
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Button Grid.Column="0" Grid.Row="0" Margin="10,10,10,10" Height="75" Click="Button_Click">Set Label Background</Button>
            <Button Grid.Column="0" Grid.Row="1" Margin="10,10,10,10" Height="75" Click="Button_Click_1">Clear Label Background</Button>
            <Label Name="TestLabel"  Grid.Row="2"  Margin="10,10,10,10" BorderBrush="Gray" BorderThickness="5"></Label>
        </Grid>

我使用的网格与您发布的网格相同

确保您在后面的代码中正确设置了控件的数据上下文,以使其正常工作。现在,即使单击任何按钮,原始样式也会保留。

请告诉我这是否有效。

关于wpf - 为什么为控件分配背景颜色会覆盖样式触发器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24761238/

相关文章:

c# - 删除有人打开文件的目录

c# - 如何使用 backgroundworker 更新 GUI?

wpf - 通过服务保存时如何保持模型一致?

c# - C#OpenFileDialog打开包含单个文件的zip文件夹?

Wpf 将 View 绑定(bind)到 ViewModel 添加到 wpf 窗口

c# - MEF - 是否必须以某种方式设置导入或者它们可能为空?

c# - 使用 Viewbox 缩放网格内的控件

wpf - 根据单元格的内容设置 DataGridRow 的背景

c# - DynamicDataDisplay WPF - 获取当前视口(viewport)中可见的数据

c# - WPF 将集合的集合绑定(bind)到 DataGrid