I'm trying to specify the design-time DataContext of all my .xaml files in order to get maximum use out of Resharper and its ability to refactor and warn about non-existing bindings. I'm having trouble with a non-generic collection.
Keep in mind throughout this post that everything works perfectly at runtime, this is purely a design-time issue.
Consider the following snippets:
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>
The problem with this setup is I get the following warnings on the bindings of the columns: Cannot resolve property 'Name' in context of type 'object'.
If I change MyViewModel.cs
to have a property IList<MyClass> ItemsToDisplay
instead, Resharper will successfully recognise that the collection has items of type MyClass
and resolve the datacontext properly, getting rid of the warnings. However, because of code constraints, I cannot change the type of the collection.
So my question is: Is there some hidden xaml magic that will allow me to do something like this and specify the type of the collection? Pseudocode:
<telerik:RadGridView ItemsSource="{Binding ItemsToDisplay, d:ItemsSourceType=my:MyClass}">
I would suggest you look into TypeDescriptor 's and create a custom type descriptor for your item type. Not sure this will work in your specific case but it's a possible route to try at least.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.