数据绑定到代码中的CLR属性
  绑定到依赖属性在代码隐藏中很容易。  您只需创建一个新的System.Windows.Data.Binding对象,然后调用目标依赖项对象的SetBinding方法。 
  但是,当我们绑定的属性是一个CLR属性并且你不能向SetBinding提供一个DependencyProperty参数时,你怎么做到这SetBinding ? 
  编辑:该对象实现INotifyPropertyChanged ,如果这是相关的。 
绑定目标必须是依赖属性! 这是数据绑定工作的唯一要求!
在这里阅读更多:
  如果我正确理解你的问题,你有一个FrameworkElement公开一个普通的旧的普通属性,它不作为一个Dependency属性备份。  但是,您希望将其设置为绑定的目标。 
首先让TwoWay绑定工作不太可能,在大多数情况下是不可能的。 但是,如果您只想要一种方式绑定,那么您可以创建一个附加属性作为实际属性的代理。
  让我想象我有一个StatusDisplay框架元素,它具有一个字符串Message属性,对于某些非常愚蠢的原因,不支持Message作为依赖属性。 
public static StatusDisplaySurrogates
{
    public static string GetMessage(StatusDisplay element)
    {
        if (element == null)
        {
            throw new ArgumentNullException("element");
        }
        return element.GetValue(MessageProperty) as string;
    }
    public static void SetMessage(StatusDisplay element, string value)
    {
        if (element == null)
        {
            throw new ArgumentNullException("element");
        }
        element.SetValue(MessageProperty, value);
    }
    public static readonly DependencyProperty MessageProperty =
        DependencyProperty.RegisterAttached(
            "Message",
            typeof(string),
            typeof(StatusDisplay),
            new PropertyMetadata(null, OnMessagePropertyChanged));
    private static void OnMessagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        StatusDisplay source = d as StatusDisplay;
        source.Message = e.NewValue as String;
    }
}
  当然,如果StatusDisplay控件的Message属性由于某种原因而被直接修改,这个代理的状态将不再匹配。  不过这对你的目的可能并不重要。 
等待。 你是否试图绑定2个CLR属性? 让我说这样的事情是不可能以正常方式实现的。 例如。 没有一种硬核破解可以使你的整个应用程序不稳定。 绑定的一端必须是DependencyProperty。 期。
链接地址: http://www.djcxy.com/p/48401.html上一篇: Databinding to CLR property in code
下一篇: How to have Android App communicate with external MySQL db
