简体   繁体   English

如何将ObservableCollection绑定到XAML中的IList属性?

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

I have IList property to get ItemSource of my DropDown list. 我有IList属性来获取DropDown列表的ItemSource。 How can I bind a ObservabelCollection to IList property in view(XAML)? 如何将ObservabelCollection绑定到视图中的IList属性(XAML)? I tried normal binding but it throws null reference exception. 我尝试了正常绑定,但它抛出了null引用异常。

Note: I can able to bind collection in code behind. 注意:我可以在代码后面绑定集合。 I am getting exception only when I try to bind it through Xaml. 只有当我尝试通过Xaml绑定它时才会出现异常。

I tried with collections of type ObservableCollection, IList and IEnemerable 我尝试使用ObservableCollection,IList和IEnemerable类型的集合

IList Property IList财产

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

Colletion 总汇

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

Xaml view Xaml视图

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

It's not because you aren't able to bind but it's not able to find the property . 这不是因为你无法绑定,但它无法找到属性。 Do check the BindingContext if it is set to appropriate level and postcode would really be helpful 检查BindingContext是否设置为适当的级别,并且邮政编码确实会有所帮助

To add Binding feature to your Application you can use trick by adding Loaded event in constructor of your Xaml Page and inside this event you need add the same code below in OnLoaded Event . 要向应用程序添加绑定功能,可以通过在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