首页 > 解决方案 > 通过 WPF VS20019 中的 xaml 设计数据

问题描述

我一直在努力尝试使示例数据脱离 XAML。我尝试使用本指南https://blogs.msdn.microsoft.com/wpfsldesigner/2010/06/30/sample-data-in-the-wpf-and-silverlight-designer/和本指南https://docs .microsoft.com/en-us/windows/uwp/data-binding/displaying-data-in-the-designer以获取有关该主题的信息,但除了这些页面之外,我还没有找到任何其他信息足够好的来源. 为了尝试理解这种模式,我制作了一个简单的 WPF 项目来测试它。

<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" d:DataContext="{d:DesignData Source=DesignData.xaml}">
    <Window.DataContext>
        <local:Viewmodel/>
    </Window.DataContext>
    <Grid>
        <TextBlock HorizontalAlignment="Left" Margin="119,104,0,0" TextWrapping="Wrap" Text="{Binding TextBlockValue}" VerticalAlignment="Top"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="255,101,0,0" TextWrapping="Wrap" Text="{Binding TextboxValue}" VerticalAlignment="Top" Width="120"/>
        <Border Margin="542,71,80,223" BorderThickness="2">
            <Border.BorderBrush>Black</Border.BorderBrush>
            <ItemsControl ItemsSource="{Binding Persons}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding FirstName}"/>
                            <TextBlock Text="{Binding Lastname}"/>
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Border>
    </Grid>
</Window>

这是我的简单 WPF 窗口,它有一个文本框、文本块和一个 ItemsControl。它有一个带有 Viewmodel 的 DataContext 集和一个设计数据 DataContext。视图模型如下:

public class Viewmodel : INotifyPropertyChanged
{
    public Viewmodel()
    {
        Persons = new ObservableCollection<Person>();
        Persons.Add(new Person{FirstName = "first one", Lastname = "last one"});
        Persons.Add(new Person{FirstName = "John", Lastname = "Doe"});
        Persons.Add(new Person{FirstName = "Jane", Lastname = "Doe"});

        TextBlockValue = "This is a textBlock";
        textboxValue = "This is a textBox";
    }

    private string textBlockValue;
    public string TextBlockValue
    {
        //<Omitted for readability>
    }

    private string textboxValue;
    public string TextboxValue
    {
        //<Omitted for readability>
    }

    public ObservableCollection<Person> Persons { get; set; }   

    //<Omitted INotifyPropertyChanged implementation for readability>
}

public class Person : INotifyPropertyChanged
{
    private string firstName;
    public string FirstName
    {
         //<Omitted for readability>
    }

    private string lastname;
    public string Lastname
    {
        //<Omitted for readability>
    }

    public event PropertyChangedEventHandler PropertyChanged;

    //<Omitted INotifyPropertyChanged implementation for readability>
}

还有我的设计数据:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfApp1">
    <local:Viewmodel TextboxValue="Box Test" TextBlockValue="Block test" x:Key="Viewmodel">
        <local:Viewmodel.Persons>
            <local:Person Lastname="test" FirstName="test"/>
            <local:Person Lastname="test" FirstName="test"/>
            <local:Person Lastname="test" FirstName="test"/>
        </local:Viewmodel.Persons>
    </local:Viewmodel>
</ResourceDictionary>

When I add my ViewModel datacontext to the xaml I can see that the values show up in the designer. But when I assign my d:datacontext, the test data does not appear as expected. I think it is because my design data is wrong, but I cannot figure out why it is wrong.

标签: wpf

解决方案


The contents of your DesignData.xaml file should look like this, i.e. it shouldn't contain a ResourceDictionary:

<local:Viewmodel xmlns:local="clr-namespace:WpfApp1" TextboxValue="Box Test" TextBlockValue="Block test">
    <local:Viewmodel.Persons>
        <local:Person Lastname="test" FirstName="test"/>
        <local:Person Lastname="test" FirstName="test"/>
        <local:Person Lastname="test" FirstName="test"/>
    </local:Viewmodel.Persons>
</local:Viewmodel>

You may also want to set the Build Action of the file to DesignData.


推荐阅读