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

21,803

Solution 1

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}"/>

Solution 2

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:

enter image description here

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>

enter image description here

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="[email protected]" />
            <local:MockRecord FirstName="Wilma" LastName="Flintstone" Email="[email protected]" />
            <local:MockRecord FirstName="Barney" LastName="Rubble" Email="[email protected]" />
        </x:Array>
    </ResourceDictionary>
</Window.Resources>
<Grid>
    <DataGrid  Margin="10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
               ItemsSource="{Binding Source={StaticResource MyDumbMockedList}}"/>
</Grid>

Solution 3

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="[email protected]" />
     <local:MockRecord FirstName="Wilma" LastName="Flintstone" Email="[email protected]" />
     <local:MockRecord FirstName="Barney" LastName="Rubble" Email="[email protected]" />
</DataGrid>
Share:
21,803
Gary Barrett
Author by

Gary Barrett

Updated on August 03, 2020

Comments

  • Gary Barrett
    Gary Barrett over 3 years

    I have static data that I want to display in Datagrid format. The values are for display purposes only and will not change. Can it be added as some kind of subtag of the Datagrid control so I can avoid anything in the codebehind?

    It has to be Datagrid control only as the purpose is to experiment with and demo certain Datagrid UI features with dummy blah blah content.

    If pure XAML content is not possible then what is the best (quick & dirty) method to set up dummy content for a datagrid? Can it be done without writing classes etc?