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

后端 未结 3 1259
伪装坚强ぢ
伪装坚强ぢ 2020-12-31 04:41

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 Dat

相关标签:
3条回答
  • 2020-12-31 04:54

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

    0 讨论(0)
  • 2020-12-31 04:55

    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}"/>
    
    0 讨论(0)
  • 2020-12-31 04:56

    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>
    
    0 讨论(0)
提交回复
热议问题