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?
You can make entire dataGridView or entire column or entire row as ReadOnly . dataGridView1. ReadOnly = true; dataGridView1.
WPF DataGrid (SfDataGrid) provides built-in row called AddNewRow. It allows user to add a new row to underlying collection. You can enable or disable by setting SfDataGrid.
A DataGrid is a control that displays data in a customizable grid. It provides a flexible way to display a collection of data in rows and columns. The hierarchical inheritance of DataGrid class is as follows −
DataGrid is used to display data in scrollable grid. It requires data source to populate data in the grid. It is a server side control and can be dragged from the toolbox to the web form. Data Source for the DataGrid can be either a DataTable or a database.
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="[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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With