繁体   English   中英

为非通用集合的项目指定设计时类型

[英]Specifying design-time type for items of non-generic collection

我试图指定所有.xaml文件的设计时DataContext,以最大程度地利用Resharper及其重构和警告不存在的绑定的功能。 我在使用非通用集合时遇到了麻烦。

请记住,在这篇文章中,所有内容都可以在运行时完美运行,这纯粹是设计时的问题。

请考虑以下片段:

MyList.cs

public class MyList : IList { ... }

MyClass.cs

public class MyClass {
    public string Name { get; set; }
    public string Description { get; set; }
}

MyViewModel.cs

public class MyViewModel {
    public MyList ItemsToDisplay { get; set; } // filled with items of type MyClass
}

MyWindow.xaml

<Window x:Class="MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
        xmlns:my="clr-namespace:MyNameSpace"
        mc:Ignorable="d" d:DataContext="{d:DesignInstance my:MyViewModel}">

    <telerik:RadGridView ItemsSource="{Binding ItemsToDisplay}">
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" Header="Name"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Description}" Header="Description"/>
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>

</Window>

此设置的问题是,我在列的绑定上收到以下警告: Cannot resolve property 'Name' in context of type 'object'.

如果我将MyViewModel.cs更改为具有属性IList<MyClass> ItemsToDisplay ,则Resharper将成功识别出该集合具有MyClass类型的项,并正确解析了数据上下文,从而摆脱了警告。 但是,由于代码限制,我无法更改集合的类型。

所以我的问题是:是否存在一些隐藏的xaml魔术,可以让我做这样的事情并指定集合的​​类型? 伪代码:

<telerik:RadGridView ItemsSource="{Binding ItemsToDisplay, d:ItemsSourceType=my:MyClass}">

我建议您调查TypeDescriptor并为您的项目类型创建一个自定义类型描述符。 不确定这是否会在您的特定情况下起作用,但至少可以尝试一下。

暂无
暂无

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

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