XAML设计器显示绑定的属性名称而不是属性值

XAML设计器显示绑定的属性名称而不是属性值

本文介绍了WPF XAML设计器显示绑定的属性名称而不是属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个XAML文件在XAML设计器中显示出奇怪的行为(但在运行时未显示):

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 { ... } }
}

然后使用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>

在XAML设计器中,两个TextBlock实例显示以下内容:

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

MyText
0

XAML设计器显示字符串属性的属性名称,对于int属性,它显示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.

我已经重新启动了Visual Studio和PC,并删除了.suo文件.

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

对此行为有解释吗?

推荐答案

结果是缺少的东西是IsDesignTimeCreatable属性:

Turns out the missing thing was the IsDesignTimeCreatable attribute:

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

有了该属性,一切都会按预期进行.

With that attribute, everything works as expected.

这篇关于WPF XAML设计器显示绑定的属性名称而不是属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:46