I'm trying to use design time DataContext in a UserControl. According to this article (and some posts on this site) it should be possible to use the design time DataContext in the UserControl tag itself.
However it's not working for me, neither in the UserControl's nor in the root element's tag.
The UserControl (and the mockup view model) are pretty simple. A bunch of primitive types bound to some text blocks and sliders.
My UserControl looks like:
<UserControl mc:Ignorable="d"
d:DataContext="{d:DesignInstance mockups:PrototypeMockup}">
<GroupBox DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Path=Prototype}" Header="{Binding Name}" >
...
<Image Source="{Binding Image}" />
<TextBlock Text="{Binding Size, StringFormat={}{0} mm}" />
<Slider Minimum="10" Maximum="80" Value="{Binding Size}" IsEnabled="{Binding IsEditing}" />
<TextBlock Text="{Binding Threshold}" />
<Slider Minimum="0" Maximum="255" Value="{Binding Threshold}" IsEnabled="{Binding IsEditing}" />
<TextBlock Text="{Binding BlockSize, StringFormat={}{0} px}" />
<Slider Minimum="10" Maximum="200" Value="{Binding BlockSize}" IsEnabled="{Binding IsEditing}" />
...
and the mockup view model looks like:
public class PrototypeMockup
{
public string Name => "Mockup Template";
public int Threshold => 230;
public int Size => 50;
public int BlockSize => 120;
public bool Ignore => false;
public bool IsEditing => true;
public Int32Point ImageCenter => new Int32Point(400, 200);
public BitmapSource Image => new BitmapImage(new Uri("image.png"));
}
The UserControl in the designer doesn't update the bindings if I put the d:DataContext
in the UserControl tag, but it does update bindings if I put it in the GroupBox (root element) tag.
However it does not use the values I put in the mockup class, but insted the default values of each property in that class.
What do I have to do to get the values of the mockup class into my view?
Edit
I'm using .net 4.6
Update
It seems like the code is valid. I've created a new project and added an exact copy of the control and it works just perfectly. However it still doesn't work in the real project.
What I've tried:
- deleting .suo file
- deleting designers shadow cache
- clean & rebuild
- reboot