Click here to Skip to main content
12,551,121 members (48,660 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# XAML WPF Binding
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:
    <!-- 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>
    <!-- an array of other type of content -->
    <x:Array x:Key="myarray2" Type="{x:Type StackPanel}">
          <Ellipse Width="16" Height="16" Fill="Red"/>
          <TextBlock Text="Item 1"/>
          <Ellipse Width="16" Height="16" Fill="Blue"/>
          <TextBlock Text="Item 2"/>
          <Ellipse Width="16" Height="16" Fill="Green"/>
          <TextBlock Text="Item 3"/>
  <!-- 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}"
    <!-- bind button's content to the combo selected item -->
    <Button Content="{Binding /}"
            Margin="5" />

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

1 solution

Rate this: bad
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"/>

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 | Mobile
Web02 | 2.8.161021.1 | Last Updated 5 Dec 2012
Copyright © CodeProject, 1999-2016
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