2010-06-24 78 views
3

我有一个DataTemplate如下内的组合框:WPF组合框和的SelectedItem绑定到XML数据源

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

任何线索上述代码有什么问题。 在此先感谢。

+0

什么上面的代码? – decyclone 2010-06-24 14:06:14

+0

我的意思是xaml代码 – Jhelumi786 2010-06-24 14:18:38

+0

好吧,我已经设法解决这个问题,但现在我有另一个。 我有三个组合框显示来自同一个源文件(xml)的图像。用户从每个组合框中挑选图像作为选择1,2 3. 我将这3个选择绑定到产品数据源(xml文件)中的三个不同字段,如Logo1,Logo2,Logo3),但所有三个comboboexs以某种方式被绑定一起。如果我从选区1中选取图像,则其他两个组合框将更新选区1。 – Jhelumi786 2010-06-24 15:48:39

回答

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 [email protected]}" 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; 
     } 
    } 
    }