簡體   English   中英

如何將ObservableCollection綁定到XAML中的IList屬性?

[英]How to bind ObservableCollection to IList property in XAML?

我有IList屬性來獲取DropDown列表的ItemSource。 如何將ObservabelCollection綁定到視圖中的IList屬性(XAML)? 我嘗試了正常綁定,但它拋出了null引用異常。

注意:我可以在代碼后面綁定集合。 只有當我嘗試通過Xaml綁定它時才會出現異常。

我嘗試使用ObservableCollection,IList和IEnemerable類型的集合

IList財產

    public IList ItemsSource
    {
        get { return this.itemsSource; }
        set { this.itemsSource = value; }
    }

總匯

    public IList<Address> AddressSource
    {
        get { return this.address; }
        set { this.address = value; }
    } 

Xaml視圖

<DataForm Editor="DropDown" ItemsSource="{Binding AddressSource}" Name="Country"/>

這不是因為你無法綁定,但它無法找到屬性。 檢查BindingContext是否設置為適當的級別,並且郵政編碼確實會有所幫助

要向應用程序添加綁定功能,可以通過在Xaml頁面的構造函數中添加Loaded事件來使用技巧,在此事件中,您需要在OnLoaded事件中添加以下相同的代碼。

public MainPage()
{
    this.InitializeComponent();
    this.Loaded += OnLoaded;
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
    this.DataContext = this;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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