内部管理DependencyProperty值和更改的回调 [英] Internal management of DependencyProperty value and Changed callbacks

查看:53
本文介绍了内部管理DependencyProperty值和更改的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是关于如何管理依赖项属性的基本误解,但我似乎找不到一个明确的例子来纠正我。



请看以下内容代码示例...

 公共类MyControl 
{
public static readonly DependencyProperty ExpressionProperty =
DependencyProperty.Register( Expression,
typeof(Expression),
typeof(MyControl),
new PropertyMetadata(ExpressionChanged));

public Expression Expression
{
get {return(Expression)GetValue(ExpressionProperty); }
set {SetValue(ExpressionProperty,value); }
}

私有静态无效ExpressionChanged(DependencyObjectdependencyObject,DependencyPropertyChangedEventArgsdependencyPropertyChangedEventArgs)
{
...必须响应属性
的外部更改。 。更新用户界面以反映对属性
}的外部更改

private void RespondToInput()
{
...对表达式执行操作,添加新元素或执行某些操作b $ b ...现在表达式已更改,因此我想更新依赖项属性
...,以便datacontext获取新值。
SetValue(ExpressionProperty,updatedExpression);
}
}

我不明白的是,当我这样做时在RespondToInput工作时,我现在想更新DependencyProperty,但如果这样做,则调用PropertyChanged回调,这时我走了一圈,现在就开始更新UI,即使我已从UI有效地发起了更改。 / p>

我不知道这是否足够合理。



我做错了什么?



谢谢!

解决方案

您不能阻止在以下情况下调用PropertyChangedCallback属性值更改。您可以做的是对内部属性更改不做出反应

  private bool isInternalExpressionChanged; 

私有静态无效ExpressionChanged(DependencyObjectdependencyObject,DependencyPropertyChangedEventArgsdependencyPropertyChangedEventArgs)
{
if(!isInternalExpressionChanged)
{
...
}
}

private void RespondToInput()
{
...
isInternalExpressionChanged = true;
SetValue(ExpressionProperty,updatedExpression);
isInternalExpressionChanged = false;
}


I think this is a basic misunderstanding about how to manage dependency properties but I can't seem to find a clear example to correct me.

Looking at the following code as an example...

public class MyControl
{
    public static readonly DependencyProperty ExpressionProperty = 
                                    DependencyProperty.Register("Expression",
                                    typeof (Expression),
                                    typeof (MyControl),
                                    new PropertyMetadata(ExpressionChanged));

    public Expression Expression
    {
        get { return (Expression)GetValue(ExpressionProperty); }
        set { SetValue(ExpressionProperty, value); }
    }

    private static void ExpressionChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        ... Must respond to external change of property
        ... Update UI to reflect external change to property
    }

    private void RespondToInput()
    {
        ... Do something to expression, add new elements or something
        ... Now expression has changed so I want to update the dependency property
        ... so datacontext gets new value.
        SetValue(ExpressionProperty, updatedExpression);
    }
}

What I don't understand is that when I do the RespondToInput work, I want to now update the DependencyProperty, but if I do, the PropertyChanged callback is called, at which point I go in a circle and now start updating the UI, even though I initiated the change from the UI effectively.

I don't know if that makes enough sense.

What did I do wrong??

Thanks!

解决方案

You can't prevent the PropertyChangedCallback from being called when the property value changes. What you can do is not to react on an internal property change:

private bool isInternalExpressionChanged;

private static void ExpressionChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
    if (!isInternalExpressionChanged)
    {
        ...
    }
}

private void RespondToInput()
{
    ...
    isInternalExpressionChanged = true;
    SetValue(ExpressionProperty, updatedExpression);
    isInternalExpressionChanged = false;
}

这篇关于内部管理DependencyProperty值和更改的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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