3

I have a comboBox inside a DataTemplate as below:

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

What I'm trying to achieve is to update the property (ImageName) of my XML datasource with the SelectedItem of the comboxBox.

Any clue what's wrong with the above code. Thanks in advance.

Chepene
  • 1,128
  • 1
  • 12
  • 18
Jhelumi786
  • 41
  • 1
  • 5
  • Ok I have managed to solve the problem but now I'm having another one. I have three ComboBoxes showing the images from the same source file(xml). User picks image the image from each combobox as selection 1, 2 3. I'm binding those 3 selections to three different fields in the product datasource(xml file) as Logo1, Logo2, Logo3) but all three comboboexs are somehow gets binded together. If I pick up and image from selection 1, the other two combo boxes are getting updated with the selection 1. – Jhelumi786 Jun 24 '10 at 15:48

1 Answers1

1

I suggest moving your ComboBox outside of the DataTemplate, and do your ComboBox customisation inside an ItemTemplate.

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

The following test code-behind shows different ComboxBox selected items:

  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;
        }
     }
  }
Zamboni
  • 7,897
  • 5
  • 43
  • 52