Question

I have a combobox where I want to display several objects and have enum values returned. Random number of the combobox is generated in a view according to the external data. The combobox works as expected, but after I open any of them and then another, then the one I opened for the first time, then the items in the first one are not accessible.(The items are still present but the dropdown menu is empty).

To be clear I put my actions here step by step:

  1. I open ComboBox1. I can see all items.
  2. I open ComboBox2. I can see all items.
  3. I try to open ComboBox1. There are no items in it´s dropdown menu.
  4. I open ComboBox3. I can see all items.
  5. All items are gone from both - ComboBox1 and ComboBox2.

I guess that the problem is caused by broken path to the ItemsSource, but I could not figure out how to avoid it.

<DataTemplate x:Key="CustomizedComboBox">
<StackPanel Width="70" Margin="0,0,12,0">
  <ComboBox 
      SelectedIndex="{Binding Path=Type, Mode=TwoWay, Converter={vm:SelectedItemConverter}}" 
      ItemsSource="{StaticResource IconsAndLabelsArray}" 
      MinHeight="50"/>
  </ComboBox>
</StackPanel>

IconsAndLabelsArray:

<x:Array x:Key="IconsAndLabelsArray" Type="{x:Type StackPanel}" >
    <StackPanel Orientation="Horizontal">
      <Image Height="42"
             VerticalAlignment="Top"
             Source="/ProjectName;image1.png"
             Stretch="Uniform" />
      <Label VerticalAlignment="Center" Content="Image1 Label" />
    </StackPanel>
    <StackPanel Orientation="Horizontal">
      <Image Height="42"
             VerticalAlignment="Top"
             Source="/ProjectName;image2.png"
             Stretch="Uniform" />
      <Label VerticalAlignment="Center" Content="Image2 Label" />
    </StackPanel>       

Was it helpful?

Solution

Set x:Shared="False" on IconsAndLabelsArray so that for each comboBox new resource is created instead of using previous one.

<x:Array x:Shared="False" x:Key="IconsAndLabelsArray"
         Type="{x:Type StackPanel}" >
  ....
</x:Array>

Issue is Visual must be disconnected before adding to another Visual parent. That's why combobox last opened always won and since resources are shared by default. Hence an issue.

Setting sharing to false will give each comboBox, new instance of resource.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top