Click here to Skip to main content
15,868,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I have 2 objetcs: a ComboBox and a Button. I want the button's content to show the ComboBox's selected item.

Everything is fine if I work with strings. But something strange is happening when I use something else : the combo collection is emptied as I change my selection... :(

My items are made of a StackPanel that contains an Image and a TextBlock (I replaced the Image with an Ellipse in the example but the result is the same).

Here is my code:
XML
<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">
  <Page.Resources>
    <!-- a string array -->
    <x:Array x:Key="myarray1" Type="{x:Type sys:String}">
        <sys:String>Item 1</sys:String>
        <sys:String>Item 2</sys:String>
        <sys:String>Item 3</sys:String>
        <sys:String>Item 4</sys:String>
    </x:Array>
    <!-- an array of other type of content -->
    <x:Array x:Key="myarray2" Type="{x:Type StackPanel}">
        <StackPanel>
          <Ellipse Width="16" Height="16" Fill="Red"/>
          <TextBlock Text="Item 1"/>
        </StackPanel>
        <StackPanel>
          <Ellipse Width="16" Height="16" Fill="Blue"/>
          <TextBlock Text="Item 2"/>
        </StackPanel>
        <StackPanel>
          <Ellipse Width="16" Height="16" Fill="Green"/>
          <TextBlock Text="Item 3"/>
        </StackPanel>
    </x:Array>
  </Page.Resources>
  <!-- in the DataContext, if I change myarray2 into myarray1 everything is fine -->
  <WrapPanel DataContext="{Binding Source={StaticResource myarray2}, Path=.}">
    <!-- bind combo's items to the DataContext -->
    <ComboBox ItemsSource="{Binding}"
              IsSynchronizedWithCurrentItem="true"
              Margin="5"/>
    <!-- bind button's content to the combo selected item -->
    <Button Content="{Binding /}"
            Margin="5" />
  </WrapPanel>
</Page>


Now if you change the selection of the combo, the button's content is properly updated but the item is removed from the combo's list.

Does anyone know how to fix that?
Posted
Updated 5-Dec-12 2:04am
v2

1 solution

If you know that you're always binding to the array of TextBlocks then you could set the binding path of the Button to
<Button Content="{Binding Path=Text}" Margin="5"/>


/Fredrik
 
Share this answer
 
Comments
Olivier Levrey 5-Dec-12 6:31am    
Well... your solution works of course but because you get the string of the TextBlock. I gave TextBlock just as an example.

In my app, this is a more complex object: a panel containing an image and a text. I tried to bind the text part using your approach: it works. But the image part is still "stolen"...
Fredrik Bornander 5-Dec-12 6:46am    
If you provide some details of the actual scenario you're trying to solve I might be able to suggest a fix for that.
Olivier Levrey 5-Dec-12 7:56am    
Yes you are right. Thanks. I'll update my question in a minute.
Olivier Levrey 5-Dec-12 8:05am    
I updated my question with a more detailed example. Thanks for your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900