如何使用数据触发器设置WPF行为属性 [英] How to set WPF behavior property using a data trigger

查看:35
本文介绍了如何使用数据触发器设置WPF行为属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过以下方式使用样式设置WPF行为属性:

I am trying to set a WPF behavior property using a style in the following way:

<StackPanel>
    <CheckBox Name="IsFemaleChkBox" Content="Is Female ?" />
    <TextBlock>
        <Hyperlink> <!--setting property directly like this:  local:MyHyperLinkBehavior.Salutation="Mr." isn't working either-->
            <TextBlock Text="My Hyperlink"/>
            <Hyperlink.Style>
                <Style TargetType="Hyperlink">
                    <Setter Property="local:MyHyperLinkBehavior.Salutation" Value="Mr." />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsChecked, ElementName=IsFemaleChkBox}" Value="True">
                            <Setter Property="local:MyHyperLinkBehavior.Salutation" Value="Miss" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Hyperlink.Style>
        </Hyperlink>
    </TextBlock>
</StackPanel>

行为类代码是这样的:

class MyHyperLinkBehavior : Behavior<Hyperlink>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.Click += AssociatedObject_Click;
    }

    public static bool GetIsFemale(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsFemaleProperty);
    }

    public static void SetIsFemale(DependencyObject obj, bool value)
    {
        obj.SetValue(IsFemaleProperty, value);
    }

    // Using a DependencyProperty as the backing store for IsFemale.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IsFemaleProperty =
        DependencyProperty.RegisterAttached("IsFemale", typeof(bool), typeof(MyHyperLinkBehavior), new PropertyMetadata(false));


    public static string GetSalutation(DependencyObject obj)
    {
        return (string)obj.GetValue(SalutationProperty);
    }

    public static void SetSalutation(DependencyObject obj, string value)
    {
        obj.SetValue(SalutationProperty, value);
    }

    // Using a DependencyProperty as the backing store for Salutation.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SalutationProperty =
        DependencyProperty.RegisterAttached("Salutation", typeof(string), typeof(MyHyperLinkBehavior), new PropertyMetadata(default(string)));

    void AssociatedObject_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        MessageBox.Show(Convert.ToString(GetValue(SalutationProperty)));
    }
}

我不知道为什么这不起作用.还是使用样式设置行为的属性完全无效?如果这是无效的,反过来又是怎么回事.

I can't figure out why this is not working. Or setting a behavior's property using style isn't valid at all ? What is the other way round, if this is not valid.

推荐答案

我正在使用,这对我来说是一个小小的怀念.

I got it working, it was a small miss by me.

  1. 我忘记设置超链接的行为.
  2. 我需要获取AttachedObject的属性,而不是
    行为.

以下代码可以正常工作:

Following code works fine:

<StackPanel>
    <CheckBox Name="IsFemaleChkBox" Content="Is Female ?" />
    <TextBlock>
        <Hyperlink>
            <TextBlock Text="My Hyperlink"/>
            <i:Interaction.Behaviors> <!--Missed setting behavior-->
                <local:MyHyperLinkBehavior />
            </i:Interaction.Behaviors>
            <Hyperlink.Style>
                <Style TargetType="Hyperlink">
                    <Setter Property="local:MyHyperLinkBehavior.Salutation" Value="Mr." />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsChecked, ElementName=IsFemaleChkBox}" Value="True">
                            <Setter Property="local:MyHyperLinkBehavior.Salutation" Value="Miss" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Hyperlink.Style>
        </Hyperlink>
    </TextBlock>
</StackPanel>

行为:

class MyHyperLinkBehavior : Behavior<Hyperlink>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.Click += AssociatedObject_Click;
    }

    public static bool GetIsFemale(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsFemaleProperty);
    }

    public static void SetIsFemale(DependencyObject obj, bool value)
    {
        obj.SetValue(IsFemaleProperty, value);
    }

    // Using a DependencyProperty as the backing store for IsFemale.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IsFemaleProperty =
        DependencyProperty.RegisterAttached("IsFemale", typeof(bool), typeof(MyHyperLinkBehavior), new PropertyMetadata(false));


    public static string GetSalutation(DependencyObject obj)
    {
        return (string)obj.GetValue(SalutationProperty);
    }

    public static void SetSalutation(DependencyObject obj, string value)
    {
        obj.SetValue(SalutationProperty, value);
    }

    // Using a DependencyProperty as the backing store for Salutation.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SalutationProperty =
        DependencyProperty.RegisterAttached("Salutation", typeof(string), typeof(MyHyperLinkBehavior), new PropertyMetadata(default(string)));

    void AssociatedObject_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        // Changing "GetValue(SalutationProperty)" to "this.AssociatedObject.GetValue(SalutationProperty)" works
        MessageBox.Show(Convert.ToString(this.AssociatedObject.GetValue(SalutationProperty)));
    }
}

这篇关于如何使用数据触发器设置WPF行为属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆