如何编写数据触发器以根据第二个依赖属性的更改来更改依赖属性的值?

发布于 2024-12-10 17:46:08 字数 2041 浏览 0 评论 0原文

我想更改由第二个依赖属性 (ViewType) 的更改触发的 UserControl 中依赖属性 (Mode) 的状态。

我尝试如下定义 DataTrigger,但由于某种原因它不起作用:

<UserControl.Resources>
    <Style TargetType="{x:Type UserControls:MyUserControl}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ViewType}" Value="ViewType2">
                <Setter Property="Mode" Value="Logon"/>
            </DataTrigger>
      </Style.Triggers>
    </Style>
</UserControl.Resources>

为了验证我的绑定是否正常工作,我在同一用户控件中编写了以下测试 XAML:

    <Style x:Key="butstyle">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ViewType}" Value="ViewType2">
                <Setter Property="Control.Foreground" Value="White"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

当我将此样式分配给按钮时,我看到前景色在正确的时间更改为白色,这证明我的 ViewType 绑定正在工作。

我似乎需要在 usercontrol.resources 下使用样式(如上面的第一段代码所示),但“Mode”属性从未设置。

这是正确的方法吗?如果可能的话,我想尝试将其保留在 XAML 中,但是当触发器与视觉元素不直接相关时,我不太清楚触发器应该如何工作。

如果相关,“ViewType”绑定属性在父 UserControl 中定义,“Mode”属性在 MyUserControl 上定义。

更新:

我使用转换器发现我的触发器正在被调用,但是我的设置器由于某种原因没有被调用。这是我正在尝试设置的 DP 的代码。 set() 和 OnModeChanged() 上的断点都不会被调用。我的依赖属性有什么问题吗?

    public enum ModeStates { Logon, NextState }

    public ModeStates Mode
    {
        get { return (ModeStates)GetValue(ModeProperty); }
        set { SetValue(ModeProperty, value); }
    }

    protected static readonly DependencyProperty ModeProperty = DependencyProperty.Register(
            "Mode", typeof(ModeStates), typeof(MyUserControl),
            new FrameworkPropertyMetadata(new PropertyChangedCallback(OnModeChanged))
            );


    private static void OnModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

            // .......

    }

更新:

我能够使用 DependencyPropertyDescriptor.AddValueChanged() 以非常干净的方式解决我的问题。

I want to change the state of a dependency property (Mode) in a UserControl triggered off of a change change of a second dependency property (ViewType).

I tried defining a DataTrigger as follows, but for some reason it doesn't work:

<UserControl.Resources>
    <Style TargetType="{x:Type UserControls:MyUserControl}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ViewType}" Value="ViewType2">
                <Setter Property="Mode" Value="Logon"/>
            </DataTrigger>
      </Style.Triggers>
    </Style>
</UserControl.Resources>

To verify that my binding was working correctly I wrote the following test XAML in the same user control:

    <Style x:Key="butstyle">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ViewType}" Value="ViewType2">
                <Setter Property="Control.Foreground" Value="White"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

When I assigned this style to a button, I see the foreground color change to white at the right time, which proves my Binding to ViewType is working.

I seems I need to use a style under usercontrol.resources (like in the first block of code above), but the "Mode" property is never set.

Is this the right way to do this? I'd like to try to keep this in XAML if possible, but I'm not comfortable how triggers should work when they are not related directly to visual elements.

In case its relevant, the "ViewType" bound property is defined in a parent UserControl, and the "Mode" property is defined on MyUserControl.

Update:

I used a converter to find out that my trigger is being called, however my setter is not getting engaged for some reason. Here is the code for my DP which I am trying to set. Breakpoints on neitehr the set() nor the OnModeChanged() are called. Is there anything wrong with my Dependency Property?

    public enum ModeStates { Logon, NextState }

    public ModeStates Mode
    {
        get { return (ModeStates)GetValue(ModeProperty); }
        set { SetValue(ModeProperty, value); }
    }

    protected static readonly DependencyProperty ModeProperty = DependencyProperty.Register(
            "Mode", typeof(ModeStates), typeof(MyUserControl),
            new FrameworkPropertyMetadata(new PropertyChangedCallback(OnModeChanged))
            );


    private static void OnModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

            // .......

    }

Update:

I was able to use DependencyPropertyDescriptor.AddValueChanged() to solve my problem in a pretty clean fashion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

国产ˉ祖宗 2024-12-17 17:46:08

如果相关依赖属性是在 UserControl 上定义的,您需要设置它的样式属性,而不是在资源中添加默认样式。

在 XAML 中设置内联样式

<UserControl.Style>
    <Style TargetType="{x:Type UserControls:MyUserControl}">
      <!-- Triggers -- >
    </Style>
</UserControl.Style>

If the dependency properties in question are defined on the UserControl you need to set it's style property, not add a default style inside the resources.

to set the style inline in XAML

<UserControl.Style>
    <Style TargetType="{x:Type UserControls:MyUserControl}">
      <!-- Triggers -- >
    </Style>
</UserControl.Style>
你在我安 2024-12-17 17:46:08

除了Adam 的回答,您是否在 标记中设置 Mode 属性任何地方?如果这样做,则会覆盖触发值。

另外,有时我会遇到触发值未设置的问题,除非在样式中也定义了默认值。

例如,有时这不起作用,

<UserControl.Style>
    <Style TargetType="{x:Type UserControls:MyUserControl}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ViewType}" Value="ViewType2">
                <Setter Property="Mode" Value="Logon"/>
            </DataTrigger>
      </Style.Triggers>
    </Style>
</UserControl.Style>

而这却起作用

<UserControl.Style>
    <Style TargetType="{x:Type UserControls:MyUserControl}">
        <Setter Property="Mode" Value="{x:Null}"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding ViewType}" Value="ViewType2">
                <Setter Property="Mode" Value="Logon"/>
            </DataTrigger>
      </Style.Triggers>
    </Style>
</UserControl.Style>

In addition to Adam's answer, are you setting the Mode property in the <UserControl> tag anywhere? If you do, it is overwriting the triggered value.

Also, on occasion I have had issues with a triggered value not getting set unless a default value is also defined in the style.

For example, sometimes this will not work

<UserControl.Style>
    <Style TargetType="{x:Type UserControls:MyUserControl}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ViewType}" Value="ViewType2">
                <Setter Property="Mode" Value="Logon"/>
            </DataTrigger>
      </Style.Triggers>
    </Style>
</UserControl.Style>

while this does

<UserControl.Style>
    <Style TargetType="{x:Type UserControls:MyUserControl}">
        <Setter Property="Mode" Value="{x:Null}"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding ViewType}" Value="ViewType2">
                <Setter Property="Mode" Value="Logon"/>
            </DataTrigger>
      </Style.Triggers>
    </Style>
</UserControl.Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文