wpf - 通过 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.
解决方案
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
.
推荐阅读
- wso2is - 登录 Dahsboard 时可以使用密码策略验证器 isconnector 吗?
- r - 需要将 XML 文件中的第一个工作表转换为 data.frame
- wordpress - Wordpress - 如何仅在一个特定菜单上将活动类添加到当前菜单项?
- python - 如何根据值列表的长度对python字典进行排序
- javascript - 与 A/HREF 元素类似,使用 CSS 选择器定位 JavaScript 绑定链接
- javascript - highlightjs 无法与 Showdown 和 AngularJS 一起正常工作
- maven - 无法在 IntelliJ 中使用 Maven 加载 Eclipse 运行时 jar
- python - bigquery.job.ExtractJobConfig()
- c# - C# foreach 的代码优化
- reactjs - Material UI Tab 将道具传递给组件