WPF ComboBox和SelectedItem绑定到XML数据源

3

我有一个如下所示的DataTemplate中的comboBox:

<ComboBox x:Name="cboImages" Grid.Row="1" Grid.Column="1" SelectedItem="{Binding XPath=ImageName, Path=SelectedItem.Content, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" >
   <ComboBoxItem>Image1.jpg</ComboBoxItem>
   <ComboBoxItem>Image2.jpg</ComboBoxItem>
   <ComboBoxItem>Image3.jpg</ComboBoxItem>
</ComboBox>

我想要实现的是使用下拉框的SelectedItem来更新XML数据源的属性(ImageName)。
以上代码有什么问题吗?
谢谢。

好的,我已经成功解决了问题,但现在我又遇到了另一个问题。 我有三个ComboBox,它们显示来自同一源文件(xml)的图像。用户从每个ComboBox中选择图像作为选择1、2、3。 我将这三个选择绑定到产品数据源(xml文件)中的三个不同字段(Logo1、Logo2、Logo3),但是所有三个ComboBox都会以某种方式绑定在一起。如果我从选择1中选择一个图像,其他两个ComboBox也会更新为选择1。 - Jhelumi786
1个回答

1
我建议将你的ComboBox移出DataTemplate,然后在ItemTemplate中进行ComboBox的自定义。
<Window x:Class="BindXML.Views.MainView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Main Window" Height="400" Width="800">

  <Window.Resources>
    <DataTemplate x:Key="comboTemplate">
        <TextBlock Text="{Binding XPath=@ImageName}" Width="70" />
    </DataTemplate>
    <XmlDataProvider x:Key="src" XPath="/Root">
        <x:XData>
            <Root xmlns="">
                <Item ImageName="Image1.jpg" />
                <Item ImageName="Image2.jpg" />
                <Item ImageName="Image3.jpg" />
            </Root>
        </x:XData>
    </XmlDataProvider>
  </Window.Resources>
  <DockPanel>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ComboBox x:Name="cboImages1"
                  Grid.Row="0"
                  DataContext="{StaticResource src}"
                  ItemTemplate="{StaticResource comboTemplate}"
                  ItemsSource="{Binding XPath=Item}"                      
                  SelectedItem="{Binding XPath=ImageName, Path=SelectedItem.Content, Mode=TwoWay}" 
                  IsSynchronizedWithCurrentItem="True" >
        </ComboBox>
        <ComboBox x:Name="cboImages2"
                  Grid.Row="1"
                  DataContext="{StaticResource src}"
                  ItemTemplate="{StaticResource comboTemplate}"
                  ItemsSource="{Binding XPath=Item}"                      
                  SelectedItem="{Binding XPath=ImageName, Path=SelectedItem.Content, Mode=TwoWay}" 
                  IsSynchronizedWithCurrentItem="True"  >
        </ComboBox>
        <Button Grid.Row="2" Click="Button_Click" />
    </Grid>
  </DockPanel>
</Window>

下面的测试代码显示了不同的ComboxBox选定项:

  private void Button_Click(object sender, RoutedEventArgs e)
  {
     XmlElement e1 = cboImages1.SelectedItem as XmlElement;
     if ( e1 != null )
     {
        XmlAttribute result1 = e1.Attributes["ImageName"] as XmlAttribute;
        if ( result1 != null )
        {
           string name1 = result1.Value;
        }
     }

     XmlElement e2 = cboImages2.SelectedItem as XmlElement;
     if ( e2 != null )
     {
        XmlAttribute result2 = e2.Attributes["ImageName"] as XmlAttribute;
        if (result2 != null)
        {
           string name2 = result2.Value;
        }
     }
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接