Click here to Skip to main content
13,191,877 members (54,306 online)
Rate this:
 
Please Sign up or sign in to vote.
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:
<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 4-Dec-12 23:53pm
Updated 5-Dec-12 2:04am
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.171017.2 | Last Updated 5 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100