简体   繁体   English

类型对象的UWP依赖属性未绑定到ObservableCollection

[英]UWP Dependency Property of type object not binding to ObservableCollection

I have a custom UserControl with a RadDataGrid and a ListView. 我有一个带有RadDataGrid和ListView的自定义UserControl。 The UserControl has an ItemsSource which, after some internal operations on the data, populates the ItemsSource properties of both the RadDataGrid and the ListView. UserControl有一个ItemsSource,在对数据进行一些内部操作之后,该ItemsSource填充RadDataGrid和ListView的ItemsSource属性。

Once populated, I want to expose the ItemsSource property of the RadDataGrid to my view model. 填充后,我想将RadDataGrid的ItemsSource属性公开给我的视图模型。

To do this, I create a Dependency Property "GridItemsSource" in the code behind file with a TwoWay binding in the UserControl's XAML file as: 为此,我在文件后面的代码中创建一个依赖项属性“ GridItemsSource”,并在UserControl的XAML文件中使用TwoWay绑定,如下所示:

public static readonly DependencyProperty GridItemsSourceProperty = DependencyProperty.Register(nameof(GridItemsSource), typeof(object), typeof(ListViewGrid), new PropertyMetadata(null, OnItemsSourceChanged));
public object GridItemsSource
{
    get { return GetValue(GridItemsSourceProperty); }
    set { SetValue(GridItemsSourceProperty, value); }
}

And: 和:

<grid:RadDataGrid x:Name="DataGrid"
                          Grid.Column="1"
                          UserGroupMode="Disabled"
                          AutoGenerateColumns="False"
                          ItemsSource="{x:Bind GridItemsSource, Mode=TwoWay}"
                          SelectedItem="{x:Bind SelectedGridItem, Mode=TwoWay}">
</grid:RadDataGrid>

In my page I also have a TwoWay binding to the GridItemsSource property as: 在我的页面中,我还具有GridItemsSource属性的TwoWay绑定,如下所示:

<userControls:ListViewGrid x:Name="ListViewGrid"
                           GridItemsSource="{x:Bind ViewModel.FormItems, Mode=TwoWay}"
                           SelectedGridItem="{x:Bind ViewModel.SelectedFormItem, Mode=TwoWay}"/>

And in my ViewModel: 在我的ViewModel中:

private ObservableCollection<FormItem> formItems;
public ObservableCollection<FormItem> FormItems
{
    get => formItems;
    set => Set(ref formItems, value);
}

This works well for the "SelectedGridItem" alone, but when I bind the "GridItemsSource" the program stops at the setter of the Dependency Property with the following runtime error: 这对于单独的“ SelectedGridItem”效果很好,但是当我绑定“ GridItemsSource”时,程序将在Dependency属性的设置器处停止,并出现以下运行时错误:

Unable to cast object of type 'System.Collections.Generic.List1[System.Object]' to type 'System.Collections.ObjectModel.ObservableCollection1[Models.FormItem] . Unable to cast object of type 'System.Collections.Generic.List1[System.Object]' to type 'System.Collections.ObjectModel.ObservableCollection1[Models.FormItem]

Why do I get this error? 为什么会出现此错误? All ItemsSource properties I have seen in other Controls are all of type object and binding to an ObservableCollection is not a problem with them. 我在其他控件中看到的所有ItemsSource属性都是对象类型,并且绑定到ObservableCollection并不是问题。

I think the problem is that you use Mode=TwoWay on the ItemsSource properties. 我认为问题在于您在ItemsSource属性上使用Mode=TwoWay ItemsSource property should be just "read-only" by the control. 控件应将ItemsSource属性设置为“只读”。 Try setting the GridItemsSource and ItemsSource properties to Mode=OneWay only, so that the system doesn't try to reflect any changes to the view model which could cause the conversion problem. 尝试仅将GridItemsSourceItemsSource属性设置为Mode=OneWay ,这样系统就不会尝试反映对视图模型的任何更改,而这可能会导致转换问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM