WPF XAML designer showing bound property names instead of property values
Asked Answered
S

4

4

One of my XAML files shows a strange behaviour in the XAML designer (but not during runtime):

public class MyDesignTimeViewModel
{

    public MyDesignTimeViewModel()
    {
        MyText = "abc";
        MyInt = 5;
    }

    public string MyText { get { ... } }
    public int MyInt { get { ... } }
}

Then in XAML:

<UserControl xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             ...
             d:DataContext="{d:DesignInstance vm:MyDesignTimeViewModel}"
             >
    <TextBlock Text="{Binding MyText}" />
    <TextBlock Text="{Binding MyInt}" />
</UserControl>

In the XAML designer, the two TextBlock instances show the following content:

MyText
0

The XAML designer shows the property name of the string property, and for the int property, it shows a 0.

There's no error in the error window. Furthermore, it seems to actually read the properties, because if I change the binding to a non-existing property name, the content disappears.

I've restarted Visual Studio and my PC and deleted the .suo file.

Any explanation for this behaviour?

Scheldt answered 21/6, 2016 at 14:20 Comment(0)
S
2

Turns out the missing thing was the IsDesignTimeCreatable attribute:

d:DataContext="{d:DesignInstance vm:MyDesignTimeViewModel, IsDesignTimeCreatable=true}"

With that attribute, everything works as expected.

Scheldt answered 21/6, 2016 at 14:21 Comment(1)
Another thing for folks to try: the trick for me was to delete the .vs folder (and possibly bin and obj folders). Yeesh.Graminivorous
M
4

Thankyou for the last two answers. In my case 'Enable Project Code in XAML' has randomly turned off as well! I didnt have the designer pulling the view model code for a few weeks. So very happy this has been fixed. So in all to get this working you need:

d:DataContext="{d:DesignInstance vm:MyDesignTimeViewModel}"

and the little icon with a blue square and caption 'Enable Project Code in XAML' to be turned on!

Melchor answered 6/6, 2019 at 12:58 Comment(1)
I appreciate this answer. This was a pain, mine just randomly flipped off like yours.Lor
D
3

I ran into this issue but the cause was actually that I didn't have the Enable Project Code in XAML Designer enabled.

Dour answered 9/3, 2018 at 18:57 Comment(0)
S
2

Turns out the missing thing was the IsDesignTimeCreatable attribute:

d:DataContext="{d:DesignInstance vm:MyDesignTimeViewModel, IsDesignTimeCreatable=true}"

With that attribute, everything works as expected.

Scheldt answered 21/6, 2016 at 14:21 Comment(1)
Another thing for folks to try: the trick for me was to delete the .vs folder (and possibly bin and obj folders). Yeesh.Graminivorous
C
1

To echo J.G.'s answer, the fix was to enable project code in the XAML designer which had mysteriously been disabled at some point.

New account so I can't vote or comment, I just didn't like seeing a correct answer downvoted.

Coz answered 9/5, 2018 at 18:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.