C# Silverlight中的SetBinding()会发生什么情况

C# Silverlight中的SetBinding()会发生什么情况,c#,.net,silverlight,binding,C#,.net,Silverlight,Binding,我有以下代码,这是将空值绑定到组合框的解决方案的一部分。(假设我不支持更改此实现) 问题是:这行代码是否会导致源代码更新目标,反之亦然 因此,如果视图模型中有此属性: string MyProperty { get { return _myProperty; } set { myProperty = value; IsDirty = true; } } 然后,当我调用SetBinding时,将调用setter。这将

我有以下代码,这是将空值绑定到组合框的解决方案的一部分。(假设我不支持更改此实现)

问题是:这行代码是否会导致源代码更新目标,反之亦然

因此,如果视图模型中有此属性:

 string MyProperty
 {
     get { return _myProperty; }
     set 
     {
        myProperty = value;
        IsDirty = true;
     }
 }
然后,当我调用
SetBinding
时,将调用setter。这将很麻烦,因为在setter中,我还管理'IsDirty属性以跟踪更改。我可以编码:

 string MyProperty
 {
     get { return _myProperty; }
     set 
     {  
        if (myProperty != value)
        {
           myProperty = value;
           IsDirty = true;
        }
     }
 }
但我觉得那太不雅观了,因为我有很多房子

所以我的第二个问题是(假设第一个问题的答案是肯定的),我可以强制Silverlight在
SetBinding()
之后不更新目标或源代码(这样它们只会在更改时更新)

此WorkAround的工作原理:
\u cachedBinding
因此包含在
MyProperty
变为
null
之前设置的绑定,并且绑定消失了(因此它与原来的绑定相同(但与对象不同,因为它是重新创建的))

再次查看代码,这可能是同一个对象,但这取决于
GetBindingExpression
ParentBinding
在.NET中的工作方式

下面是完全绑定的
组合框
子类:

public class ComboBoxFixedBinding : ComboBox
{
    #region Events

    private void ComboBoxEx_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        if (SelectedValue == null)
        {
            if (_cachedBinding != null && GetExistingBinding() == null)
            {
                SetBinding(ComboBox.SelectedValueProperty, _cachedBinding);
            }
        }
        else
        {
            CacheExistingBinding();
        }
    }

    private void CacheExistingBinding()
    {
        Binding binding = GetExistingBinding();

        if (binding != null)
        {
            _cachedBinding = binding;
        }
    }

    private Binding GetExistingBinding()
    {
        BindingExpression bindingExpr = this.GetBindingExpression(ComboBox.SelectedValueProperty);
        if (bindingExpr == null)
        {
            return null;
        }
        else
        {
            return bindingExpr.ParentBinding;
        }

    }

    #endregion

    public ComboBoxFixedBinding()
    {
        SelectionChanged += ComboBoxEx_SelectionChanged;
    }

    private Binding _cachedBinding;
}

为了回答这个问题,我们必须知道缓存绑定包含哪些内容。
public class ComboBoxFixedBinding : ComboBox
{
    #region Events

    private void ComboBoxEx_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        if (SelectedValue == null)
        {
            if (_cachedBinding != null && GetExistingBinding() == null)
            {
                SetBinding(ComboBox.SelectedValueProperty, _cachedBinding);
            }
        }
        else
        {
            CacheExistingBinding();
        }
    }

    private void CacheExistingBinding()
    {
        Binding binding = GetExistingBinding();

        if (binding != null)
        {
            _cachedBinding = binding;
        }
    }

    private Binding GetExistingBinding()
    {
        BindingExpression bindingExpr = this.GetBindingExpression(ComboBox.SelectedValueProperty);
        if (bindingExpr == null)
        {
            return null;
        }
        else
        {
            return bindingExpr.ParentBinding;
        }

    }

    #endregion

    public ComboBoxFixedBinding()
    {
        SelectionChanged += ComboBoxEx_SelectionChanged;
    }

    private Binding _cachedBinding;
}