问题
I have a DataGrid inside my WPF application and I would like it to contain values from my XML file. The DataGrid is shown below:
<TabItem Header="Datacopy">
<Grid>
<DataGrid x:Name="GrdDatacopy" HorizontalAlignment="Left" Margin="7,7,0,0" VerticalAlignment="Top" Height="320" Width="640">
<DataGrid.Columns>
<DataGridTextColumn DisplayMemberBinding="{Binding Path = Source}" Header="Bron:"></DataGridTextColumn>
<DataGridTextColumn DisplayMemberBinding="{Binding Path = Destination}" Header="Doel:"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</TabItem>
My XML:
<DataCopy Dir="H:\O365-Source">
<Row Source="C:\Temp" Destination="Temp" />
<Row Source="C:\Downloads" Destination="Downloads" />
<Row Source="C:\Muziek" Destination="Muziek" />
</DataCopy>
I can create a object for the datagrid
$DatacopySet = @{}
$XmlOffice365.Office365.Datacopy.Row | ForEach {
$DatacopySet.add($_.Source))
$DatacopySet.add($_.Destination))
}
$DatacopySet | ForEach-Object { .................... }
But when I paste in the secion my whole app crashes, it should be something with the bindings.
How can I get my XML keys in the datagrid and be able to edit these.
I've found some samples but I couldn't get this to work...
PS1 script http://poshcode.org/2259
XAML code: http://poshcode.org/2260
Article on a Microsoft forum
Hope that someone can help, or is there a other way to easaly edit and add values in this sort of setting?
Regards, Paul
回答1:
You can display your xml data in the following way
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApplication3"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="525"
Height="350"
mc:Ignorable="d">
<Window.Resources>
<XmlDataProvider x:Key="XmlDataCopy" XPath="DataCopy">
<x:XData>
<DataCopy xmlns="" Dir="H:\O365-Source">
<Row Destination="Temp" Source="C:\Temp" />
<Row Destination="Downloads" Source="C:\Downloads" />
<Row Destination="Muziek" Source="C:\Muziek" />
</DataCopy>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Grid>
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Source={StaticResource XmlDataCopy}, XPath='Row'}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding XPath='@Destination'}" Header="Destination" />
<DataGridTextColumn Binding="{Binding XPath='@Source'}" Header="Source" />
</DataGrid.Columns>
</DataGrid>
</Grid>
Note:
-XmlDataProvider can be used with different Sources, including URIs
-Note the empty xmlns="" in the xml-snippet. You easily run into namespace issues otherwise.
-Editing the document is also possible in case the XmlDataProvider's Source is a variable in memory. You must call XmlDocument.Save() manually.
Hope this helps a bit, regards Snowball
来源:https://stackoverflow.com/questions/34314934/put-xml-in-a-datagrid-view-inside-wpf