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?
I ran into this issue but the cause was actually that I didn't have the Enable Project Code in XAML Designer enabled.
Turns out the missing thing was the IsDesignTimeCreatable
attribute:
d:DataContext="{d:DesignInstance vm:MyDesignTimeViewModel, IsDesignTimeCreatable=true}"
With that attribute, everything works as expected.
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.
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!
来源:https://stackoverflow.com/questions/37947002/wpf-xaml-designer-showing-bound-property-names-instead-of-property-values