gpt4 book ai didi

c# - Wpf ICollectionView 绑定(bind)项无法解析类型对象的属性

转载 作者:可可西里 更新时间:2023-11-01 08:26:23 25 4
gpt4 key购买 nike

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

例如实体是 Personstring Name我把它们放在 ObservableCollection<Person> 中并从中获取 View 并将其绑定(bind)到 GridView.ItemsSource现在,当我尝试设置列标题时 DataMemberBinding.FirstName设计者将其显示为错误的属性

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

是bug还是Resharper在耍我

示例代码:

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>

enter image description here

最佳答案

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

添加以下内容(适本地替换 Assembly/Namespace/Binding Target 名称)

<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}">

关于c# - Wpf ICollectionView 绑定(bind)项无法解析类型对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20254690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com