WPF DataGridComboBoxColumn绑定到XmlDataProvider中的数据

时间:2014-11-22 21:33:01

标签: c# wpf xaml data-binding wpfdatagrid

我有一个像这样的xml数据,我想绑定一个datagridcomboboxcolum列出所有供应商 - 给定项目的姓氏(Datagrid上下文已绑定到选择项目的组合框)。

<?xml version="1.0" encoding="utf-8" ?>
<ProjectList>
  <SingleProject id="1" name="TRP">
    <LanguageList>
      <Language id ="1">
        <TargetCode>en-gb</TargetCode>
        <TargetLanguageName>English</TargetLanguageName>
        <VendorList>
          <Vendor id="1">
            <Name>Karin</Name>
            <Surname>AAA</Surname>
            <GridPath>e:\stuff</GridPath>
          </Vendor>
          <Vendor id="2">
            <Name>Kate</Name>
            <Surname>BBB</Surname>
            <GridPath>e:\stuffss</GridPath>
          </Vendor>
        </VendorList>
      </Language>
    </LanguageList>
  </SingleProject>
</ProjectList>

这是一个XAML代码:

<UserControl x:Class="Test">
    <UserControl.Resources>
        <XmlDataProvider x:Key="XmlData" Source="../!setup.xml"/>
    </UserControl.Resources>
    <Grid Style="{StaticResource ContentRoot}">
        <Grid Margin="10,10,722,280">
            <TextBlock Text="Select:" Style="{StaticResource Heading2}" Margin="0,0,34,105" />
            <ComboBox x:Name="cmbSelect" ItemsSource="{Binding Source={StaticResource XmlData}, XPath=/ProjectList/*}" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Left" Margin="0,32,0,0" VerticalAlignment="Top" Width="123" Height="28">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding XPath=@name}" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
        </Grid>
        <DataGrid HorizontalAlignment="Left" Margin="148,10,0,0" VerticalAlignment="Top" Height="215" Width="707"
                  RenderOptions.ClearTypeHint="Enabled"
                  TextOptions.TextFormattingMode="Display"                 
                  SelectionUnit="FullRow"
                  IsSynchronizedWithCurrentItem="True"
                  DataContext="{Binding ElementName=cmbSelect, Path=SelectedItem}"
                  ItemsSource="{Binding XPath=./LanguageList/Language}"
                  AutoGenerateColumns="False" GridLinesVisibility="All">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Language Code"
                                    Binding="{Binding XPath=TargetCode}"/>
                <DataGridTextColumn Header="Language Name"
                                    Binding="{Binding XPath=TargetLanguageName}"/>
                <DataGridTextColumn x:name="dtcVendorName" Header="Vendor Name"
                                    Binding="{Binding XPath=VendorList/Vendor/Name}"/>
                <DataGridTextColumn x:name="dtcVendorSurname" Header="Vendor Surname"
                                    Binding="{Binding XPath=VendorList/Vendor/Surname}"/>
                <DataGridComboBoxColumn x:name="dcbVendorList" Header="Default Vendor"                                
                                        DisplayMemberPath="{Binding XPath=*}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>

我遇到了绑定DataGridComboBoxColumn&#34; dcbVendorList&#34;的问题。列出xml中的所有供应商。 Combobox应该将一个姓氏显示为项目。

然后根据dcbVendorList中的选择,datagridtextcolumn&#34; dtcVendorName&#34;和&#34; dtcVendorSurname&#34;应填充此组合框的数据。

我试过使用Xpath *和。没有运气。似乎当前的项目是&#34;语言&#34;来自datacontext的xml节点。

<DataGridComboBoxColumn x:name="dcbVendorList" Header="Default Vendor" DisplayMemberPath="{Binding XPath=*}

非常感谢任何帮助。

0 个答案:

没有答案