簡體   English   中英

如何在后面的代碼中設置“ {Binding}”?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM