首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >样式触发器在直接SetValue后停止工作

样式触发器在直接SetValue后停止工作
EN

Stack Overflow用户
提问于 2018-02-13 01:03:16
回答 2查看 58关注 0票数 1

设置BackgroundProperty会直接破坏IsMouseOver风格的触发器。如何有可能直接设置BackgroundProperty,并保留触发器之后工作?

XAML:

代码语言:javascript
运行
复制
<Window x:Class="WpfTriggers.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <SolidColorBrush x:Key="OriginalBackground" Color="Blue"/>
        <SolidColorBrush x:Key="TemplateBackground" Color="Red"/>
        <Style TargetType="Label" >
            <Style.Setters>
                <Setter Property="Background" Value="{StaticResource OriginalBackground}"/>
                <Setter Property="Foreground" Value="White"/>
            </Style.Setters>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="{StaticResource TemplateBackground}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Label Focusable="True" x:Name="label">Hi!</Label>
            <Button Click="Button_Click">Over</Button>
        </StackPanel>
    </Grid>
   </Window>

代码隐藏:

代码语言:javascript
运行
复制
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WpfTriggers
{
    public partial class MainWindow : Window
    {
        public MainWindow() => InitializeComponent();    
        void Button_Click(object sender, RoutedEventArgs e) => label.SetValue(Control.BackgroundProperty, Brushes.Green);       
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-19 14:41:32

我们通过复制样式,用带背景设置器的克隆暂时替换它,并返回样式来解决这个问题。这是作为模仿具有完全自定义动态绑定的数据触发器的行为而投入生产的。

代码语言:javascript
运行
复制
    public static class extensions
{
    /// <summary>
    /// Shallow clone of style.
    /// </summary>
    /// <param name="original">Style to clone.</param>
    /// <returns>Cloned style.</returns>
    public static Style ShallowClone(this Style original)
    {
        var cloned = new Style(original.TargetType, original.BasedOn);
        foreach (var s in original.Setters) cloned.Setters.Add(s);
        foreach (var t in original.Triggers) cloned.Triggers.Add(t);
        cloned.Resources = original.Resources;
        return cloned;
    }
}

    Style old;

    private void Button_MouseEnter_5(object sender, System.Windows.Input.MouseEventArgs e)
    {
        old = labelWithMofidiableStyle.Style;
        Style style = old.ShallowClone();
        style.Setters.Add(new Setter { Property = Control.BackgroundProperty, Value = this.FindResource("DynamicBackground") });
        labelWithMofidiableStyle.Style = style;
    }



    private void Button_MouseLeave_5(object sender, System.Windows.Input.MouseEventArgs e)
    {
        labelWithMofidiableStyle.Style = old;
    }
票数 -1
EN

Stack Overflow用户

发布于 2018-02-13 02:57:08

您不能直接设置在样式中使用的属性(结果是您的示例)。您必须改用绑定。您的代码中添加了一些内容:

代码语言:javascript
运行
复制
    public MainWindow()
    {
        DataContext = this;
        InitializeComponent();
        SetValue(BgBrushProperty, Resources["OriginalBackground"]);
    }

    void Button_Click(object sender, RoutedEventArgs e) => SetValue(BgBrushProperty, Brushes.Green);


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

在XAML中。

代码语言:javascript
运行
复制
 <Style.Setters>
    <Setter Property="Background" Value="{Binding BgBrush}"/>
    <Setter Property="Foreground" Value="White"/>
</Style.Setters>

你也可以像这样使用DynamicResource:

代码语言:javascript
运行
复制
<Style.Setters>
    <Setter Property="Background" Value="{DynamicResource OriginalBackground}"/>
    <Setter Property="Foreground" Value="White"/>
</Style.Setters>

使用以下代码:

代码语言:javascript
运行
复制
    public MainWindow() => InitializeComponent();
    void Button_Click(object sender, RoutedEventArgs e) => Resources["OriginalBackground"] = Brushes.Green;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48751761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档