【发布时间】:2013-01-31 11:33:03
【问题描述】:
这是我的“项目”:
我要做的是将外部 .xml 文件中的数据加载到 DataGrid 中:
XElement Persons = XElement.Load(@"d:\persons.xml");
dataGrid1.DataContext = Persons;
它工作得很好,但这是一个我无法弄清楚的问题:在 DataGrid 的顶部,您可以看到 TextBox,所以我需要使用 textBox1.text 作为 DataGrid 的数据过滤器。假设如果用户输入字母“a”,在 DataGriw 中我们应该只看到 2 行,名称为 pAul,行名称为国家 russiA,因为这两行的数据中都包含字母“a”。如果您还可以帮助我包含和排除某些列的搜索能力,那就太好了。最后,如果用户点击他使用搜索按钮找到的行 - 来自销售的数据应放置在右侧的标签中。我也很高兴知道如何加载未在 DataGrid 中实际显示的所选行的数据。假设在 xml 文件中我们有关于薪水的数据,但我们没有在 DataGrid 中显示它,我们需要在用户进行行选择时将其加载到第 4 个标签。这是我的 XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="679" Width="1137">
<Grid Height="645" Name="grid1" Width="1119">
<DataGrid Height="300" ItemsSource="{Binding Path=Elements[person]}" Margin="26,42,839,297" Name="dataGrid1" Width="250">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Element[name].Value}" Header="Name" />
<DataGridTextColumn Binding="{Binding Path=Element[country].Value}" Header="Country" />
<DataGridTextColumn Binding="{Binding Path=Element[age].Value}" Header="Age" />
</DataGrid.Columns>
</DataGrid>
<TextBox Height="24" HorizontalAlignment="Left" Margin="26,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="250" />
<Label Content="Name goes here on row select" Height="28" HorizontalAlignment="Left" Margin="307,28,0,0" Name="label1" VerticalAlignment="Top" Width="188" />
<Label Content="Country goes here on row select" Height="28" HorizontalAlignment="Left" Margin="307,72,0,0" Name="label2" VerticalAlignment="Top" Width="188" />
<Label Content="Age goes here on row select" Height="28" HorizontalAlignment="Left" Margin="307,120,0,0" Name="label3" VerticalAlignment="Top" Width="188" />
</Grid>
请像绝对的初学者一样跟我说话,因为当你使用一些特别聪明的东西时,我很难理解。一步一步受到高度赞赏。谢谢...
【问题讨论】:
-
不清楚你在问什么。您是否在询问有关使用 LINQ to XML 搜索 XML 的问题?或者您是在询问一些更新 UI 的代码?
-
我在问如何使用文本框过滤数据网格中的结果。在 Windows 窗体中,我使用了数据集过滤器,但在这里我不知道该怎么做。
标签: c# wpf xml linq datagridview