【问题标题】:WPF ComboBox and SelectedItem Binding to XML DataSourceWPF ComboBox 和 SelectedItem 绑定到 XML 数据源
【发布时间】:2011-03-07 20:12:54
【问题描述】:

我在 DataTemplate 中有一个组合框,如下所示:

<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)。

任何线索上面的代码有什么问题。 提前致谢。

【问题讨论】:

  • 好的,我已经设法解决了这个问题,但现在我有另一个问题。我有三个组合框显示来自同一源文件(xml)的图像。用户从每个组合框中选择图像作为选择 1、2 3。我将这 3 个选择绑定到产品数据源(xml 文件)中的三个不同字段作为 Logo1、Logo2、Logo3),但是所有三个组合框都以某种方式绑定在一起.如果我从选择 1 中选择图像,则其他两个组合框将使用选择 1 进行更新。

标签: wpf binding combobox


【解决方案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;
        }
     }
  }

【讨论】:

    猜你喜欢
    • 2010-10-24
    • 2011-11-01
    • 2015-06-28
    • 1970-01-01
    • 2021-10-18
    • 2017-11-09
    • 2016-03-25
    • 2019-10-01
    • 2012-06-23
    相关资源
    最近更新 更多