Wpf ICollectionView 绑定项无法解析类型对象的属性 [英] Wpf ICollectionView Binding item cannot resolve property of type object

查看:26
本文介绍了Wpf ICollectionView 绑定项无法解析类型对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 XAML 设计器中绑定了一个 GridView 和一个 ICollectionView 属性未知,因为 CollectionView 中的实体已被转换进入 Object 类型并且无法访问实体属性,它运行良好没有错误但设计器将其显示为错误,如果我绑定到集合,我可以正常访问属性

I have bound a GridView with an ICollectionView in the XAML designer the properties are not known because the entity in the CollectionView have been transformed into type Object and the entity properties can't be accessed, it runs fine no error but the designer shows it as an error, if I bind to the collection I can access the properties fine

示例实体是具有 string Name 属性的 Person 我将它们放在 ObservableCollection 中并从中获取视图并将其绑定到 GridView.ItemsSource 现在当我尝试设置列标题 DataMemberBinding.FirstName 属性时,设计器将其显示为错误

Example the entity is a Person with a string Name property I place them in an ObservableCollection<Person> and get the view from it and bind it to the GridView.ItemsSource now when I try to set the column header DataMemberBinding.FirstName property the designer shows it as an error

无法解析类型对象的数据上下文中的属性FirstName"

Cannot Resolve property 'FirstName' in data Context of type object

是BUG还是Resharper在捉弄我

Is it a bug or is it Resharper playing tricks on me

示例代码:

public class Person 
{
    public string FirstName{
       get { return _firstName; }
       set { SetPropertyValue("FirstName", ref _firstName, value); }
    }
}
public class DataService 
{
    public IDataSource DataContext { get; set; }
    public ICollectionView PersonCollection{ get; set; }

    public DataService()
    {
        DataContext = new DataSource();
        //QueryableCollectionView is from Telerik 
        //but if i use any other CollectionView same thing
        //DataContext Persons is an ObservableCollection<Person> Persons
        PersonCollection = new QueryableCollectionView(DataContext.Persons);
    }
}

<telerik:RadGridView x:Name="ParentGrid" 
    ItemsSource="{Binding DataService.PersonCollection}"
    AutoGenerateColumns="False">
    <telerik:RadGridView.Columns >
        <telerik:GridViewDataColumn Header="{lex:Loc Key=FirstName}"  
            DataMemberBinding="{Binding FirstName}"/>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

推荐答案

Resharper 在 XAML 视图中给你的警告是因为控件的设计时视图不知道它的数据上下文是什么类型.您可以使用 d:DesignInstance 来帮助您进行绑定.

The warnings that Resharper is giving you in the XAML view is because the design-time view of the control does not know what type it's data-context is. You can use a d:DesignInstance to help with your bindings.

添加以下内容(适当替换程序集/命名空间/绑定目标名称)

Add the following (replacing Assembly/Namespace/Binding Target names appropriately)

<UserControl x:Class="MyNamespace.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup‐compatibility/2006"
mc:Ignorable="d"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:lcl="clr‐namespace:MyAssembly"
d:DataContext="{d:DesignInstance Type=lcl:ViewModel}">

这篇关于Wpf ICollectionView 绑定项无法解析类型对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆