在代码

时间:2017-10-11 17:56:32

标签: c# .net xml wpf combobox

问题 无法在后面的cs代码中从XmlDataProvider填充WPF ComboBox。

背景 我们有一个WPF窗口作为更大的桌面WPF应用程序的一部分。该窗口显示ListView,其数据是从外部xml文件获取的。

目标是能够根据ComboBox中的选择过滤ListView。

使用Window_Loaded中的XmlDataProvider填充ListView。

ListView有四列:

  • 报名日期
  • 项目名称
  • 节点
  • 条目

我们需要能够按项目名称过滤列表。

XML文件需要是外部的,因为由用户决定要查看哪个XML文件。

WPF XAML代码:

    <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="106*"/>
        <ColumnDefinition Width="227*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="17*"/>
        <RowDefinition Height="73*"/>
    </Grid.RowDefinitions>
    <Label Content="DAL File Location" HorizontalAlignment="Left" Margin="46,10,0,0" VerticalAlignment="Top"/>
    <TextBox x:Name="DALLocation" HorizontalAlignment="Left" Height="23" Margin="170,13,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="258" Grid.ColumnSpan="2"/>
    <Button x:Name="DALBrowse" Content="Browse" HorizontalAlignment="Left" Margin="235,13,0,0" VerticalAlignment="Top" Width="75" Grid.Column="1" Click="DALBrowse_Click"/>
    <ListView x:Name="dal_list" HorizontalAlignment="Stretch" Height="Auto" Margin="6,6,6,6" Grid.Row="1" VerticalAlignment="Stretch" Width="Auto" Grid.ColumnSpan="2" ItemsSource="{Binding}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Entry Date" Width="120" DisplayMemberBinding="{Binding XPath=entry_date}" />
                <GridViewColumn Header="Project" Width="120" DisplayMemberBinding="{Binding XPath=project_name}" />
                <GridViewColumn Header="Node" Width="120" DisplayMemberBinding="{Binding XPath=node_name}" />
                <GridViewColumn Header="Entry" Width="120" DisplayMemberBinding="{Binding XPath=entry_text}" />
                <GridViewColumn Width="0" DisplayMemberBinding="{Binding XPath=sort_date}" />
            </GridView>
        </ListView.View>
    </ListView>
    <Label Content="Project" HorizontalAlignment="Left" Margin="46,41,0,0" VerticalAlignment="Top"/>
    <ComboBox x:Name="cbProjects" Grid.ColumnSpan="2" HorizontalAlignment="Left" Margin="170,41,0,0" VerticalAlignment="Top" Width="120" ItemsSource="{Binding  XPath=project_name}" DisplayMemberPath="@Name" SelectedValuePath="@Name"/>

</Grid>

代码背后

    public partial class DALManager : Window
{
    public string dalFolder;
    public string dalFile;
    public DALManager()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.DALLocation.Text = dalFolder;
        XmlDataProvider xmlData = new XmlDataProvider();
        xmlData.Source = new Uri(dalFolder + dalFile);
        xmlData.XPath = "DAL/entry";
        dal_list.DataContext = xmlData;
        cbProjects.DataContext = xmlData;

    }

    private void DALBrowse_Click(object sender, RoutedEventArgs e)
    {
        System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
        fbd.ShowNewFolderButton = false;
        if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            this.DALLocation.Text = fbd.SelectedPath;
            string oldPath = CGlobals.dal_path + "\\DAL.xml";
            string newPath = fbd.SelectedPath + "\\DAL.xml";
            File.Move(oldPath, newPath);
            RegistryKey rgKey = Registry.CurrentUser.CreateSubKey("SOFTWARE\\SCP-Computers\\DocuMinder");
            CGlobals.dal_path = fbd.SelectedPath;
            using (RegistryKey
               rgInfo = rgKey.CreateSubKey("UserInfo"))
            {
                rgInfo.SetValue("dal_path", CGlobals.dal_path);
            }

        }

    }
}

XML文件

    <?xml version="1.0" encoding="utf-8"?>
<DAL>
  <entry id="fe08125d-c785-40fc-816a-1e5ed3f06c7f-1">
    <sort_date>2017-10-01</sort_date>
    <entry_date>01/10/2017</entry_date>
    <project_id>0</project_id>
    <project_name Name="Dal Test">DAL Test</project_name>
    <node_name>notes</node_name>
    <entry_text>here is line 1 changed</entry_text>
  </entry>
  <entry id="fe08125d-c785-40fc-816a-1e5ed3f06c7f-2">
    <sort_date>2017-10-03</sort_date>
    <entry_date>03/10/2017</entry_date>
    <project_id>0</project_id>
    <project_name Name="Dal Test">DAL Test</project_name>
    <node_name>notes</node_name>
    <entry_text>Here is line 2</entry_text>
  </entry>
  <entry id="fe08125d-c785-40fc-816a-1e5ed3f06c7f-3">
    <sort_date>2017-10-10</sort_date>
    <entry_date>10/10/2017</entry_date>
    <project_id>0</project_id>
    <project_name Name="Dal Test">DAL Test</project_name>
    <node_name>notes</node_name>
    <entry_text>Here is line 3 </entry_text>
  </entry>
  <entry id="fe08125d-c785-40fc-816a-1e5ed3f06c7f-4">
    <sort_date>2017-10-11</sort_date>
    <entry_date>11/10/2017</entry_date>
    <project_id>0</project_id>
    <project_name Name="Dal Test">DAL Test</project_name>
    <node_name>notes</node_name>
    <entry_text>Added line 4</entry_text>
  </entry>
  <entry id="d9832a83-4aaa-4dd1-8c19-e469f1e5e1ed-1">
    <sort_date>2017-10-11</sort_date>
    <entry_date>11/10/2017</entry_date>
    <project_id>0</project_id>
    <project_name Name="Dal Test">DAL Test</project_name>
    <node_name>Another Node With Notes</node_name>
    <entry_text>Here is a note in another node.</entry_text>
  </entry>
  <entry id="b0e36598-26e1-4204-b11a-665da02ff121-1">
    <sort_date>2017-10-11</sort_date>
    <entry_date>11/10/2017</entry_date>
    <project_id>0</project_id>
    <project_name Name="Dal Test 2">DAL Test 2</project_name>
    <node_name>Testing DAL in another project</node_name>
    <entry_text>This is another project.</entry_text>
  </entry>
  <entry id="280743c7-c7e0-4345-9be3-fc502e3d1a45-1">
    <sort_date>2017-10-11</sort_date>
    <entry_date>11/10/2017</entry_date>
    <project_id>0</project_id>
    <project_name Name="Dal Test 2">DAL Test 2</project_name>
    <node_name>Test new node</node_name>
    <entry_text>Test if node name gets include when new.</entry_text>
  </entry>
  <entry id="5a660158-3d56-477f-935b-d8270560cd1d-1">
    <sort_date>2017-10-11</sort_date>
    <entry_date>11/10/2017</entry_date>
    <project_id>0</project_id>
    <project_name Name="Dal Test 2">DAL Test 2</project_name>
    <node_name>Finding new node name</node_name>
    <entry_text>Add the note text. </entry_text>
  </entry>
  <entry id="414844c2-7498-4794-9267-1abcc99de3c3-1">
    <sort_date>2017-10-11</sort_date>
    <entry_date>11/10/2017</entry_date>
    <project_id>0</project_id>
    <project_name Name="Dal Test 2">DAL Test 2</project_name>
    <node_name>Test new new node</node_name>
    <entry_text>Here is the text</entry_text>
  </entry>
</DAL>

结果

我只获得了ComboBox中列出的第一个项目名称。

我已经在XAML文件中尝试了各种组合,以根据stackoverflow上的许多其他问题绑定ComboBox,但我找不到任何适用于我的情况。

提前致谢。

0 个答案:

没有答案