views:

40

answers:

2

Hi Everyone, I have a comboBox as below. What I want is to bind the selectedItem value to a Text property of a datacontext so that another DataTemplate can show the Image. Please note that the Combobox and Target Image elements are on two different DataTemplates so that's why I need to update the Text Property (ImageName) ofDataContext at backend.

<ComboBox x:Name="cboOverlay" Grid.Row="0" Grid.Column="1" SelectedIndex="0" >
  <ComboBoxItem  Name="BC_OL" IsSelected="True">
       <StackPanel Orientation="Horizontal">
          <Image Source="Images\BC_OL.jpg"  Width="100" Height="25" Canvas.Top="0" Canvas.Left="0" />
        <TextBlock Width="100" VerticalAlignment="Center" TextAlignment="Center"><Bold>Image1</Bold></TextBlock>
       </StackPanel>
  </ComboBoxItem>
  <ComboBoxItem Name="Indian_OL">
      <StackPanel Orientation="Horizontal">
          <Image Source="Images\Indian_OL.jpg"  Width="100" Height="25" Canvas.Top="0" Canvas.Left="0" />
       <TextBlock Width="100" VerticalAlignment="Center" TextAlignment="Center"><Bold>Image2</Bold></TextBlock>
     </StackPanel>
  </ComboBoxItem>
 </ComboBox>
    <Image Source="{Binding Path=Image}" Width="81" Height="25" Canvas.Top="0" Canvas.Left="0" />
A: 

You can set each item to have a specific data context.

I'm not sure in your xaml which item you are trying to set, I think it's the last line the <Image Source="{Binding Path=Image' ... />

You can just specify the data context for that control.

Edit In reponse to comments

Since you are trying to get the selected item from the combo box and trying to send that over to the image, why not just pass it in as a value into the class that is holding the image.

I think you could do this in xaml but I'm not entirely sure how to do it.

msarchet
Jhelumi786
My ComboBox is hardcoded but I want to update the one of the Property of DataContext when the Selection Changes with the SeletctedItem.Name value. Anybody can help please in doing that? OR How can I bind an element (Text) with two sources, one is getting value from the UI element (cobBox) and updates the backend property. All in Xaml
Jhelumi786
So you need to set the ImageName Property to the name of the ComboBox, and they are both bound to the same data context?
msarchet
Well the ComboBox is not bound to anything , it has static values in it. What I need is when user choose an image from the combobox, i want to pass the name of image (Text) to backend DataContext property so that it can be used to choose the image and send the BitmapIamge element to another DataTemplate element.Or U can say that I need to bind an element with two different DataContexts. One for getting the values , other for updating the values.
Jhelumi786
A: 

It seems like you're trying to do something like this:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <DockPanel>
    <TextBox DockPanel.Dock="Top" x:Name="Source">x1</TextBox>
    <ComboBox DockPanel.Dock="Top" x:Name="myComboBox" 
       SelectedValue="{Binding ElementName=Source, Path=Text, Mode=TwoWay}">
      <sys:String>1</sys:String>
      <sys:String>22</sys:String>
      <sys:String>333</sys:String>
      <sys:String>4444</sys:String>
    </ComboBox>
    <TextBlock DockPanel.Dock="Top" 
       Text="{Binding ElementName=myComboBox, Mode=OneWay, Path=SelectedItem.Length}"/>
  </DockPanel>
</Page>

The ComboBox is bound to the text of the TextBox using two-way binding, so when you select an item from the ComboBox it updates the TextBox, and when you type a value into the TextBox that's in the ComboBox's list it changes the selected item in the ComboBox.

The TextBlock is bound to a property of the selected item in the ComboBox. Whenever the selected item changes, whether because the user selected a new one or the value in the TextBox changed, the TextBlock gets updated.

But I'm confused by all your talk about data contexts. None of the objects in the example you posted have data contexts.

Robert Rossney