16

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

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

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

4

3 回答 3

34

这是绑定在数据网格上的纯 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>

结果:

在此处输入图像描述

我无法使用 XmlDataProvider 自动生成列(我可能遗漏了一些东西):

<Grid DataContext="{Binding Source={StaticResource MockList}}">
    <DataGrid HorizontalAlignment="Stretch" Margin="10" VerticalAlignment="Stretch" 
              ItemsSource="{Binding Mode=Default, XPath=/MockObjects/MockObject}">
    </DataGrid>
</Grid>

在此处输入图像描述

但是使用像 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>

于 2014-02-12T15:09:40.820 回答
19

您可以在 XAML 中处理静态数据,是的,但是您需要为记录格式创建一个简单的类。例如,您可以创建这个类文件:

namespace TestNamespace
{
    public class MockRecord
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
    }
}

现在在您的 XAML DataGrid 中,您可以执行以下操作:

<DataGrid xmlns:local="clr-namespace:TestNamespace">

    <DataGrid.Columns>
        <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" />
        <DataGridTextColumn Header="Rate" Binding="{Binding LastName}" />
        <DataGridTextColumn Header="Cost" Binding="{Binding Email}" />
    </DataGrid.Columns>

     <!-- Static Data which will automatically go in the datagrid -->
     <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" />
</DataGrid>
于 2013-08-06T20:45:04.323 回答
6

检查此MSDN 页面上的示例部分

由于datagrid使用了类似于Combobox或ListBox的ItemsControl,所以datagrid应该是相同的逻辑。在该示例中,他们基本上在纯 XAML 中创建了一个完整的对象集合。

<XmlDataProvider x:Key="Employees" XPath="/Employees/*">
  <x:XData>
    <Employees xmlns="">
      <Employee Name="Terry Adams" Type="FTE" EmployeeNumber="1" />
      <Employee Name="Claire O&apos;Donnell" Type="FTE" EmployeeNumber="12345" />
      <Employee Name="Palle Peterson" Type="FTE" EmployeeNumber="5678" />
      <Employee Name="Amy E. Alberts" Type="CSG" EmployeeNumber="99222" />
      <Employee Name="Stefan Hesse" Type="Vendor" EmployeeNumber="-" />
    </Employees>
  </x:XData>
</XmlDataProvider>

<DataTemplate x:Key="EmployeeItemTemplate">
  <TextBlock Text="{Binding XPath=@Name}" />
</DataTemplate>


...

<ListBox Name="employeeListBox"
         ItemsSource="{Binding Source={StaticResource Employees}}"
         ItemTemplate="{StaticResource EmployeeItemTemplate}"
         SelectedValue="12345"
         SelectedValuePath="@EmployeeNumber"/>

<TextBlock Text="{Binding ElementName=employeeListBox, 
                  Path=SelectedValue}"/>
于 2012-04-20T01:34:32.827 回答