wpfxamlxaml-designer

WPF XAML designer showing bound property names instead of property values


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?


Solution

  • Turns out the missing thing was the IsDesignTimeCreatable attribute:

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

    With that attribute, everything works as expected.