5

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
Timo
  • 9,269
  • 2
  • 28
  • 58
  • I guess you should remove DataContext binding from your GroupBox element. – Evk Mar 01 '17 at 12:41
  • I have already tried it. Did not help. – Timo Mar 01 '17 at 12:46
  • Well if I just copy-paste your code into new user control (and remove DataContext from GroupBox, plus make xaml actually valid) - works fine for me. Don't forget to also build project after you make changes for designer to reflect them. – Evk Mar 01 '17 at 12:49
  • Hmm. I removed the binding and recompiled but the result is the same. Where did you put the `d:DataContext`? – Timo Mar 01 '17 at 12:57
  • No wait, you are right, it does not work as I said it does. – Evk Mar 01 '17 at 13:00
  • I have no clue why this isn't working. I removed the binding to the data context and put the design time data context in the user control (the group box has a grid as child in the actual user control). Does VS cache some things here that it's just not updating correctly? – Timo Mar 01 '17 at 13:02
  • At the risk of asking an obvious question, does your user control have a property named `Prototype`? Nothing is binding to any property of `UserControl.DataContext`; all the bindings are in the `GroupBox`. And that `Prototype` property is the one thing here that I don't see code for. – 15ee8f99-57ff-4f92-890c-b56153 Mar 01 '17 at 13:41
  • @EdPlunkett Yes the property exists. I already removed this binding for testing but it didn't help. – Timo Mar 01 '17 at 13:46
  • Does it seem to not work for usercontrols only? – Ole K Aug 24 '21 at 12:51

0 Answers0