[英]How to set “{Binding}” in code behind?
Thomas Levesque有一個不錯的控件 :
<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 });
}
}
我究竟做錯了什么?
不要設置綁定的Source
屬性。 您也沒有在XAML中這樣做。
XAML表達式Data="{Binding}"
的等效項是
BindingOperations.SetBinding(this, DataProperty, new Binding());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.