I'm binding an UI to an interface (which is implemented by several presenters, not accessible from the UI assembly).
I really like d:DesignInstance in designer because it (kind of) makes xaml strongly typed using R#.
Sadly, d:DesignInstance does not support interface types: "Cannot create an instance of an interface."
The first thing I thought is: Ok, no problem, let's create a custom markup extension which takes a System.Type as parameter, and which ProvideValue method returns a fake instance of it (dummy implementation of this interface, generated by dynamic IL emission).
This works quite well, bindings are resolved at design-time (I can see that in the design panel since my markup extension fills the object properties with a lorem-ipsum)
BUT the nicest R# feature dont work: Resharper does not recognize datacontext type, and just gives a message "Cannot resolve property '{0}' in data context of type 'object'"
Does someone know how to fix this ?
(any alternative which would allow me to let R# know about an interface datacontext type would be great)
Thanks !
ps: I also tried to create another markup extension which returns the generated runtime type in order to give it to DesignInstance: "{d:DesignInstance Type={utilsUi:InstanceType commons:User}}" => Gives the error "Object of type 'InstanceType' cannot be converted to type 'System.Type'" ... seems that DesignInstance does not support inner markup extensions :(
I've found out how it's possible to overcome this illogical Visual Studio XAML designer error.
Instead of
<SomeControl d:DataContext={d:DesignInstance commons:IUser}>
<!--element content here-->
</SomeControl>
You can write
<SomeControl>
<d:SomeControl.DataContext>
<x:Type Type="commons:IUser" />
</d:SomeControl.DataContext>
<!--element content here-->
</SomeControl>
Yes, this solution doesn't look as cool but definitely it isn't much worse.
Type
tag allows specifying interfaces, nested types (e.g. n:A+B) and even generics!
In case of generics just add a backtick and number of type arguments to your type: <x:Type Type="commons:User`1" />
.
BTW all of these also works with styles, <d:Style.DataContext>
doesn't produce errors!
You can take advantage of IsDesignTimeCreatable in this case like so:
d:DataContext="{d:DesignInstance commons:User, IsDesignTimeCreatable=False}"
which essentially instructs the designer to only use the type for intellisense and error highlighting instead of actually trying to instantiate a design instance.
I have just investigated more or less the same question... Actually, what I did is to follow the principles of MvvMLight. Precisely, I used a ViewModelLocator (which will be more or less static) so that the "right" ViewModel is injected at runtime or at design time. The magic lies in the function ViewModelBase.IsInDesignModeStatic provided by the MvvMLight framework. Finally, my ViewModelLocator class looks like
public class ViewModelLocator
{
private static readonly IKernel _kernel;
static ViewModelLocator()
{
_kernel = new StandardKernel();
if (ViewModelBase.IsInDesignModeStatic)
{
_kernel.Bind<IBasicVM>().To<DesignBasicVm>();
}
else
{
_kernel.Bind<IBasicVM>().To<BasicVm>();
}
}
public IBasicVM BasicVm { get { return _kernel.Get<IBasicVM>(); } }
}
You may ignore the Ninject _kernel but you may need it (or a similar Ioc) if you are building your ViewModels with an IoC.
The App.xaml declares the ViewModelLocator as a ressource
<Application.Resources>
<ResourceDictionary>
<viewModel:ViewModelLocator x:Key="ViewModelLocator" />
</ResourceDictionary>
</Application.Resources>
The MainWindow.DataContext property is bound to the BasicVM member of the ViewModelLocator. The Text property is bound the the GetContent member of the interface IBasicVM which is recognized statically by R# (at least R# 7.1 with VS2012)
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
DataContext="{Binding BasicVm, Source={StaticResource ViewModelLocator}}"
>
<Grid>
<TextBlock Text="{Binding GetContent}"/>
</Grid>
</Window>
You may check this repository which I have created as a template.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With