Can static data for Datagrid rows be defined purely in XAML i.e. no code behind?

ぐ巨炮叔叔 提交于 2019-11-30 08:32:57

Check the example section on this MSDN page

Since the datagrid uses ItemsControl similar to Combobox or ListBox, datagrid should be the same logic. In that example they basically create a whole collection of objects in pure 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}"/>

Here is pure XAML static data binded on a datagrid:

<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>

Result:

I wasn't able to autogenerate columns using XmlDataProvider (I'm probably missing something):

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

But using a code Behind class like Dave Suggestion allow AutoBinding to work and in my opinion is much simpler (I preferred the ResourceDictionary approach though) :

Code:

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>

You can do static data in XAML, yes, but you will need to create a simple class for the record format. For example, you could create this class file:

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

Now in your XAML DataGrid you can do this:

<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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!