将XML放在WPF中的DataGrid视图中 [英] Put XML in a DataGrid view inside WPF

查看:133
本文介绍了将XML放在WPF中的DataGrid视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WPF应用程序中有一个DataGrid,我希望它包含我的XML文件中的值。 DataGrid如下所示:

 < TabItem Header =Datacopy> 
< Grid>
< DataGrid x:Name =GrdDatacopyHorizo​​ntalAlignment =LeftMargin =7,7,0,0VerticalAlignment =TopHeight =320Width =640>
< DataGrid.Columns>
< DataGridTextColumn DisplayMemberBinding ={Binding Path = Source}Header =Bron:>< / DataGridTextColumn>
< DataGridTextColumn DisplayMemberBinding ={Binding Path = Destination}Header =Doel:>< / DataGridTextColumn>
< /DataGrid.Columns>
< / DataGrid>
< / Grid>
< / TabItem>

我的XML:

 < DataCopy Dir =H:\O365-Source> 
< Row Source =C:\TempDestination =Temp/>
< Row Source =C:\DownloadsDestination =Downloads/>
< Row Source =C:\MuziekDestination =Muziek/>
< / DataCopy>

我可以为datagrid创建一个对象

  $ DatacopySet = @ {} 
$ XmlOffice365.Office365.Datacopy.Row | ForEach {
$ DatacopySet.add($ _。Source))
$ DatacopySet.add($ _。Destination))
}
$ DatacopySet | ForEach-Object {....................}

但是当我粘贴到我的整个应用程序崩溃时,它应该是绑定的东西。



如何在datagrid中获取我的XML密钥,可以编辑这些。



我找到了一些样本,但我无法让它工作...



PS1脚本 http: //poshcode.org/2259



XAML代码: http://poshcode.org/2260



Microsoft论坛上的文章



希望有人可以帮助,或者有其他方法来简单地编辑和添加这个一些设置?



问候,Paul

解决方案

您可以按以下方式显示您的xml数据

 < Window x:Class =WpfApplication3.MainWindow
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/演示文稿
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
宽度=525
高度=350
mc:Ignorable =d>
< Window.Resources>
< XmlDataProvider x:Key =XmlDataCopyXPath =DataCopy>
< x:XData>
< DataCopy xmlns =Dir =H:\O365-Source>
< Row Destination =TempSource =C:\Temp/>
< Row Destination =下载Source =C:\Downloads/>
< Row Destination =MuziekSource =C:\Muziek/>
< / DataCopy>
< / x:XData>
< / XmlDataProvider>
< /Window.Resources>
< Grid>
< DataGrid AutoGenerateColumns =FalseItemsSource ={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>



注意:



-XmlDataProvider可以与不同的源一起使用,包括URI



- 在xml-snippet中注明空的xmlns = 。您很容易遇到命名空间问题。



- 如果XmlDataProvider的Source是内存中的一个变量,也可以编辑文档。您必须手动调用XmlDocument.Save()。



希望这有所帮助,关于Snowball


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

解决方案

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

这篇关于将XML放在WPF中的DataGrid视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆