简体   繁体   English

C#WPF绑定值不会在用户控件中触发动画

[英]C# WPF Binding value does not trigger animation to in user control

Hello I have issue with binding to user control animation, after I bind data to user control(which is bool type) it sets correct values to user control data, but does not trigger animation, I tried to use PropertyChangedCallback but with no luck user control code below: 您好,我在绑定用户控件动画时遇到问题,在我将数据绑定到用户控件(布尔类型)后,它为用户控件数据设置了正确的值,但没有触发动画,我尝试使用PropertyChangedCallback但没有运气的用户控件下面的代码:

        private static Switch_box AppWindow;

    public Switch_box()
    {
        InitializeComponent();
        AppWindow = this;
    }


    public static readonly DependencyProperty CheckboxStatusProperty = DependencyProperty.Register(nameof(CheckboxStatus), typeof(bool), typeof(Switch_box), new PropertyMetadata(false, new PropertyChangedCallback(OnCurrentReadingChanged)));//cant remove static otherwise throws error

    public bool CheckboxStatus
    {
        get
        {
            return (bool)GetValue(CheckboxStatusProperty);
        }
        set
        {
           /* if (value == true)
            {
                ((Storyboard)FindResource("OnChecking")).Begin(this);
            }
            else
            {
                ((Storyboard)FindResource("OnUnchecking")).Begin(this);
            }*/
            SetValue(CheckboxStatusProperty, value);
        }
    }


    private static void OnCurrentReadingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)//cant remove static due to PropertyChangedCallBack requires static otherwise it throws error
    {
        AppWindow.OnChecking((bool)d.GetValue(CheckboxStatusProperty));
    }

    private void OnChecking(bool Status)
    {
        switch (Status)
        {
            case true:
                {
                    ((Storyboard)FindResource("OnChecking")).Begin(this);
                    break;
                }
            case false:
                {
                    ((Storyboard)FindResource("OnUnchecking")).Begin(this);
                    break;
                }
        }
    }

And my usercontrol bind line: 和我的用户控件绑定线:

<local:Switch_box Tag="{Binding Index,IsAsync=True}" Checked="Switch_box_Checked" Unchecked="Switch_box_Unchecked" CheckboxStatus="{Binding IsEnabled,IsAsync=True}"/>

How to trigger animation after CheckboxStatus variable is changed? 更改CheckboxStatus变量后如何触发动画?

EDIT 1: updated code. 编辑1:更新的代码。

There is a naming convention. 有一个命名约定。 _StatusBox should be named CheckboxStatusProperty , and it should be public: _StatusBox应该命名为CheckboxStatusProperty ,并且应该是公共的:

public static readonly DependencyProperty CheckboxStatusProperty =
    DependencyProperty.Register(
        nameof(CheckboxStatus), typeof(bool), typeof(Switch_box),
        new PropertyMetadata(false, OnCurrentReadingChanged));

You must not call anything else than GetValue and SetValue in the CLR wrapper of a dependency property. 在依赖项属性的CLR包装器中,除GetValueSetValue ,您不得调用任何其他内容。 And you call the methods on the current instance, not on a static field: 然后您在当前实例上而不是在静态字段上调用方法:

public bool CheckboxStatus
{
    get { return (bool)GetValue(CheckboxStatusProperty); }
    set { SetValue(CheckboxStatusProperty , value); }
}

In the PropertyChangedCallback it is pointless to set the property another time. 在PropertyChangedCallback中,再次设置属性毫无意义。 And again, you should operate on the current DependencyObject instance, ie d , not on a static field: 同样,您应该对当前的DependencyObject实例(即d ,而不要对静态字段进行操作:

private static void OnCurrentReadingChanged(
    DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    ((Switch_box)d).OnChecking((bool)e.NewValue);
}

private void OnChecking(bool status)
{
    if (status)
    {
        ((Storyboard)FindResource("OnChecking")).Begin(this);
    }
    else
    {
        ((Storyboard)FindResource("OnUnchecking")).Begin(this);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM