gpt4 book ai didi

wpf - Datagrid 行的静态数据是否可以纯粹在 XAML 中定义,即没有代码背后?

转载 作者:行者123 更新时间:2023-12-03 15:06:38 25 4
gpt4 key购买 nike

我有想要以 Datagrid 格式显示的静态数据。这些值仅用于显示目的,不会更改。是否可以将其添加为 Datagrid 控件的某种子标签,以便我可以避免代码隐藏中的任何内容?

它必须是 Datagrid 控件,因为其目的是试验和演示具有虚拟内容的某些 Datagrid UI 功能。

如果纯 XAML 内容是不可能的,那么为数据网格设置虚拟内容的最佳(快速和肮脏)方法是什么?可以在不编写类(class)等的情况下完成吗?

最佳答案

这是绑定(bind)在数据网格上的纯 XAML 静态数据:

<Window x:Class="WpfStaticDataBinding.XMLWindows"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="XMLWindows" Height="152" Width="294">
<Window.Resources>
<XmlDataProvider x:Key="MockList" XPath="/MockObjects/*" >
<x:XData >
<MockObjects xmlns="">
<MockObject Name="Louis" Type="TTTT" Number="1" />
<MockObject Name="Joseph" Type="TTTT" Number="2" />
<MockObject Name="Papineau" Type="ZZZZ" Number="3" />
</MockObjects>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource MockList}}">
<DataGrid HorizontalAlignment="Stretch" Margin="10" VerticalAlignment="Stretch"
ItemsSource="{Binding Mode=Default, XPath=/MockObjects/MockObject}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding XPath=@Name}" ></DataGridTextColumn>
<DataGridTextColumn Header="Type" Binding="{Binding XPath=@Type}"></DataGridTextColumn>
<DataGridTextColumn Header="Number" Binding="{Binding XPath=@Number}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>

结果:

enter image description here

我无法使用 XmlDataProvider 自动生成列(我可能遗漏了一些东西):
<Grid DataContext="{Binding Source={StaticResource MockList}}">
<DataGrid HorizontalAlignment="Stretch" Margin="10" VerticalAlignment="Stretch"
ItemsSource="{Binding Mode=Default, XPath=/MockObjects/MockObject}">
</DataGrid>
</Grid>

enter image description here

但是使用像 Dave Suggestion 这样的代码隐藏类允许 AutoBinding 工作,并且在我看来要简单得多(我更喜欢 ResourceDictionary 方法):

代码:
namespace WpfStaticDataBinding
{
public class MockRecord
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
}

XAML
<Window x:Class="WpfStaticDataBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfStaticDataBinding"
Title="MainWindow" Height="157" Width="302">
<Window.Resources>
<ResourceDictionary>
<x:Array x:Key="MyDumbMockedList" Type="local:MockRecord">
<local:MockRecord FirstName="Fred" LastName="Flintstone" Email="fred@noemail.org" />
<local:MockRecord FirstName="Wilma" LastName="Flintstone" Email="wilma@noemail.org" />
<local:MockRecord FirstName="Barney" LastName="Rubble" Email="barney@noemail.org" />
</x:Array>
</ResourceDictionary>
</Window.Resources>
<Grid>
<DataGrid Margin="10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
ItemsSource="{Binding Source={StaticResource MyDumbMockedList}}"/>
</Grid>

关于wpf - Datagrid 行的静态数据是否可以纯粹在 XAML 中定义,即没有代码背后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10230790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com