有一个来自托马斯·莱韦斯克的好的控制:
<local:BindingProxy x:Key="proxy" Data="{Binding}" />我想将Data初始化隐藏到构造函数中,只需像这样使用它:
<local:BindingProxy x:Key="proxy" />下面是我不工作的尝试(在链接模中完成代码):
public class BindingProxy : Freezable
{
#region Overrides of Freezable
protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
}
#endregion
public object Data
{
get { return (object)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}
// Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
public BindigProxy()
{
BindingOperations.SetBinding(this, DataProperty, new Binding { Source = this });
}
}我做错了什么?
发布于 2018-01-24 11:35:19
不要设置绑定的Source属性。在XAML中也没有这样做。
与XAML表达式Data="{Binding}"等价的是
BindingOperations.SetBinding(this, DataProperty, new Binding());https://stackoverflow.com/questions/48421358
复制相似问题