WPF XAML designer showing bound property names instead of property values

这一生的挚爱 提交于 2020-01-02 08:12:19

问题


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?


回答1:


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




回答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.




回答3:


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.




回答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!



来源:https://stackoverflow.com/questions/37947002/wpf-xaml-designer-showing-bound-property-names-instead-of-property-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!