Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 触发器与依赖属性的关系_C#_.net_Wpf_Triggers_Dependency Properties - Fatal编程技术网

C# 触发器与依赖属性的关系

C# 触发器与依赖属性的关系,c#,.net,wpf,triggers,dependency-properties,C#,.net,Wpf,Triggers,Dependency Properties,我有一个关于依赖属性值的前置性的问题。 My.xaml看起来如下所示: <Window x:Class="WpfTests.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local ="clr-namespace:WpfTests"

我有一个关于依赖属性值的前置性的问题。 My.xaml看起来如下所示:

<Window x:Class="WpfTests.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local ="clr-namespace:WpfTests"
    Title="MainWindow" Height="350" Width="525">
<Window.Style>
    <Style>
        <!--<Setter Property="Canvas.Background" Value="Gray"/>-->
        <Style.Triggers>
            <Trigger Property="local:MainWindow.IsMouseOver" Value="True">
                <!--<Setter Property="local:LeistenPfeil.Symbolfarbe" Value="Red"/>-->
                <Setter Property="local:MainWindow.Cursor" Value="Hand" />
                <Setter Property="local:MainWindow.BG" Value="Blue"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Style>

<Grid Background="{Binding BG,Mode=TwoWay}">
    <Button Content="ChangeBG" HorizontalAlignment="Center" VerticalAlignment="Center" Click="OnClick"/>
</Grid>
因此,启动时背景设置为黑色(默认为DP)。当鼠标位于背景上时,背景变为蓝色。当涉及到我的codebehind中BG属性的更改时,触发器确实起作用,但它对网格背景的影响消失了。 我已经读过这篇关于MSDN的文章:

我理解的问题是:为什么触发器在BG属性有其默认值时工作,而在代码隐藏中更改时不工作?=>背景的最高频率是网格的本地背景绑定,那么为什么触发器可以工作呢


在代码隐藏中更改BG属性后,如何使触发器重新工作?

您的代码更改实际上被视为本地更改。从MSDN上的链接页面:

本地值。本地值可以通过“wrapper”属性设置,这也等同于在XAML中设置为属性或属性元素,或者通过使用特定实例的属性调用SetValue API来设置。如果使用绑定或资源设置本地值,则这些值的优先级都与设置直接值的优先级相同

我强调了相关部分,该部分指出,像您那样调用
SetValue
(通过
BG
CLR属性)将导致本地更改。由于本地更改的优先级高于
触发器
,因此它将否决
触发器
值。

而不是

BG = Brushes.Green;
它设置了一个比触发器优先级更高的本地值,您可以编写

SetCurrentValue(BGProperty, Brushes.Green);
发件人:

。。。SetCurrentValue方法更改 属性,但现有触发器、数据绑定和样式将 继续工作


绑定
Background=“{binding BG,Mode=TwoWay}”
未设置BG属性的值。这里BG是绑定的源,而不是目标。感谢您的快速响应:),我已经读过了,但可能我不明白重点。。。在PropertyMetada中设置依赖项属性的默认值或多或少是对该属性的SetValue的调用,不是吗?如果是这样的话,那么触发器从一开始就有较低的进动。我想知道元数据设置为黑色是否与调用SetValue不同?请再次查看。。。默认属性值位于列表的最后一个:依赖项属性元数据的默认值。所以,其他一切都可以覆盖它。哦,我必须读过这篇文章,它清楚地站在那里。。。非常感谢你!是否有解决方法,以便我可以重新激活触发器?感谢您的回复:),我的目的是更改属性的来源。当我按下按钮时,如果鼠标不在,我希望网格背景为绿色,如果鼠标不在,我希望网格背景为蓝色。因此,我可以说,要么我可以在代码中更改源代码并释放触发器,要么我可以使用SetCurrentValue()更改属性,但从长远来看,不能将背景更改为绿色。对吗?
SetCurrentValue(BGProperty, Brushes.Green);