DesignInstance不显示分配给的示例数据

时间:2018-10-01 16:43:36

标签: c# wpf

我正在使用Visual Studio 2017 15.8.5,但我无法使DesignInstance工作。 我启动了一个空白项目对其进行测试,它向我显示了属性的名称,而不是我为其分配的示例数据。

enter image description here

这是我的用户控件:

public partial class SampleUserControl : UserControl
{
    public SampleUserControl()
    {
        InitializeComponent();
    }
}

用户控件的XAML代码:

<UserControl x:Class="***.GUI.SampleUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:***.GUI"
             mc:Ignorable="d" 
             d:DesignHeight="100" d:DesignWidth="300" Background="White"
             d:DataContext="{d:DesignInstance local:SampleViewModelWithData,
                                                CreateList=False,
                                                IsDesignTimeCreatable=True}">
    <UserControl.DataContext>
        <local:SampleViewModel />
    </UserControl.DataContext>
    <Grid>
        <TextBlock Text="{Binding Value}" 
                   TextAlignment="Center" 
                   VerticalAlignment="Center" 
                   FontSize="32" />
    </Grid>
</UserControl>

视图模型:

class SampleViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string m_Value = string.Empty;
    public string Value
    {
        get => m_Value;
        set
        {
            m_Value = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Value"));
        }
    }

    public SampleViewModel() { }
}

以及具有一些数据的视图模型的派生类:

class SampleViewModelWithData : SampleViewModel
{
    public SampleViewModelWithData() : base()
    {
        Value = "ABC";
    }
}

我在做什么错了?

0 个答案:

没有答案