2010-06-24 113 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; 
     } 
    } 
    }