绑定 - 回调被调用两次 [英] Binding - callback is called twice

查看:171
本文介绍了绑定 - 回调被调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的控制与依赖属性像这样

I've a simple control with dependency property like this

public class StatusProgress : Control
{
    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(StatusProgress),
        new FrameworkPropertyMetadata(null, (d, e) => (d as StatusProgress).TextUpdated(e.OldValue as string)));

    private void TextUpdated(string text)
    {
        Trace.WriteLine("test");
    }
}

那么我有视图模型

public class ViewModelPageAnalyse : ViewModelPageBase
{

    private string _progressText;
    public string ProgressText
    {
        get { return _progressText; }
        set
        {
            _progressText = value;
            OnPropertyChanged(); // base class INotifyPropertyChanged implementation
        }
    }
}

然后有一个用户控件(显示在 ContentControl 的窗口中)。用户控件必须使用数据模板查看模型(也许这很重要)

Then there is a user control (displayed in window with ContentControl). User control is bound to view model with data template (maybe this is important)

<DataTemplate DataType="{x:Type local:ViewModelPageAnalyse}">
    <local:UserControlAnalyse/>
</DataTemplate>

这是用户控制中的控制

<local:StatusProgress Text="{Binding ProgressText}"/>

现在有趣的部分。当视图模型中的 ProgressText 设置/更改时,属性更改回调被调用两次。我在调试器输出窗口中看到两次test

Now the interesting part. When ProgressText in view model is set/changed, property changed callback is called twice. I see twice "test" in the debugger output window.

更有趣的是:当视图更改时,原因回调用 e.NewValue = null 再次调用,而没有直接将 ProgressText 设置为 null

More interesting: when view is changed, for some reason callback is again called with e.NewValue = null, while there is nothing directly sets ProgressText to null.

我已经尝试检查 ProgressText setter before rising event,试图设置绑定模式单向,问题仍然 - 回调被调用相同的值两次,调用堆栈看起来相同,但是真正有很多呼叫在wpf内真的肯定。

I tried already to check if value is changed in the ProgressText setter before rising event, tried to set binding mode one-way, problem still - callback is called twice with same value, call stack looks same, but there are really a lot of calls within wpf to be really sure.

虽然双击不是一个真正的问题,它困扰我。回调与 null 价值是什么我的真正的问题(我认为他们是相关的)。

While double-shot is not a real issue, it bother me. Callback with null value is what my real problem (I think they are related). Anyone knows what is wrong?

找到第一个问题的原因:它是其他控制与 Content 。在转换期间,它创建了一个新的模型(因为 Content ViewModel ),而不是重新分配现有的用户控制。完全是我的错。第二个问题仍然存在,我发现了问题(解决方法不适合我)。

Found a reason of the first problem: it was other control with Content. During transition it created a new Model (because Content is ViewModel) instead of reassigning existing user control. Totally my fault. Second problem still and I found this question (with workaround which is not suiting me).

需要帮助

b $ b

这意味着 null for Text 我的情况。任何人?我不知道为什么叫它。我猜这是由 DataTemplate 经理调用,如果我可以这样说。

Which means null for Text in my case. Anyone? I couldn't figure out why is it called. My guess it is called by DataTemplate manager, if I can say so.

推荐答案

尝试更改此行:

  public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text", typeof(string), typeof(StatusProgress),
    new FrameworkPropertyMetadata(null, (d, e) => (d as StatusProgress).TextUpdated(e.OldValue as string)));

    public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text", typeof(string), typeof(StatusProgress)
    , new PropertyMetadata(""));  

这篇关于绑定 - 回调被调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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