可以静态的数据网格行纯粹XAML来定义数据,即没有后面的代码?(Can static data fo

2019-06-23 18:55发布

我有我想在Datagrid的格式显示静态数据。 该值仅用于显示目的,不会改变。 它可以作为某种DataGrid控件的子标签的,所以我能避免代码隐藏什么?

它是只的目的是试验和演示某些Datagrid的UI功能与假等等等等内容Datagrid控件。

如果纯XAML内容是不可能的,那么什么是最好的(快速和肮脏的)方法来设置为DataGrid虚拟内容? 是否可以无需编写类等做了什么?

Answer 1:

检查例如一节的MSDN页

由于数据网格使用的ItemsControl类似于COMBOBOX或列表框,数据网格应该是相同的逻辑。 在那个例子中,他们基本上是建立在纯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}"/>


Answer 2:

这里是绑定在一个DataGrid纯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>

但是,使用像背后戴夫建议类允许自动绑定工作,在我看来,一个代码就简单多了(我更喜欢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>



Answer 3:

你可以在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>


文章来源: Can static data for Datagrid rows be defined purely in XAML i.e. no code behind?