[英]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.