データグリッドにバインドされた純粋な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>
しかし、Dave SuggestionのようなコードBehindクラスを使用すると、AutoBindingが機能し、私の意見でははるかに簡単です(私はこの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>