SL 5打破了我的组合框

问题描述:

我有以下xaml在SL 4中工作正常,但在SL 5插件下运行时似乎被打破了。

I have the following xaml that works fine in SL 4, but seems to be broken when run under SL 5 plug-in.

            <StackPanel 
               Orientation="Horizontal"                        
               Grid.Column="1" 
               Grid.Row="3" 
               Margin="2"
               Visibility="{Binding HasCategory1, Converter={StaticResource BooleanToVisibility}}"
               >
               
               <ComboBox 
                  x:Name="Category1Combo"
                  DataContext="{Binding Categories}"
                  ItemsSource= "{Binding Category1Choices}" 
                  SelectedItem="{Binding SelectedCategory1, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True }"
                  DisplayMemberPath="Name" 
                  />

               <ComboBox 
                  DataContext="{Binding Categories}"
                  ItemsSource= "{Binding SubCategory1List }" 
                  SelectedItem="{Binding SelectedSubCategory1, Mode=TwoWay}"
                  DisplayMemberPath="Name" 
                  Margin="5,0,0,0"
                  Visibility="{Binding SelectedItem, ElementName=Category1Combo, Converter={StaticResource HasSubCategoriesToVisibility}}"
                  >
               </ComboBox>
            </StackPanel>

            <StackPanel 
               Orientation="Horizontal"                        
               Grid.Column="1" 
               Grid.Row="4" 
               Margin="2"
               Visibility="{Binding HasCategory2, Converter={StaticResource BooleanToVisibility}}"
               >
               <ComboBox 
                  x:Name="Category2Combo" 
                  DataContext= "{Binding Categories}"
                  ItemsSource= "{Binding Category2Choices}" 
                  SelectedItem="{Binding SelectedCategory2, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True }"
                  DisplayMemberPath="Name"
                  />
               <ComboBox 
                  DataContext= "{Binding Categories}"
                  ItemsSource= "{Binding SubCategory2List }" 
                  SelectedItem="{Binding SelectedSubCategory2, Mode=TwoWay}"
                  DisplayMemberPath="Name" 
                  Margin="5,0,0,0"
                  Visibility="{Binding SelectedItem, ElementName=Category2Combo, Converter={StaticResource HasSubCategoriesToVisibility}}"
                  />
            </StackPanel>

在两个StackPanel中,ItemSource和SelectedItem在加载过程中都设置为值。 没有值为null。 当第二对组合框设置了SelectedSubCategory2时,我在SL 5下运行时出错。

In both StackPanels, the ItemSource and SelectedItem are set to values during the load process.  No values are null.  When the second pair of comboboxes has its SelectedSubCategory2 set, I get an error when running under SL 5.

错误说:

Unhandled Error in Silverlight Application 
Code: 4004    
Category: ManagedRuntimeError       
Message: System.ArgumentException: GeneratorPosition '-1,1' passed to Remove does not have Offset equal to 0.

Parameter name: position

   at System.Windows.Controls.ItemContainerGenerator.Remove(GeneratorPosition position, Int32 count, Boolean isRecycling)

   at System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.Remove(GeneratorPosition position, Int32 count)

   at System.Windows.Controls.ComboBox.SetContentPresenter(Int32 index)

   at System.Windows.Controls.ComboBox.OnApplyTemplate()

   at System.Windows.FrameworkElement.OnApplyTemplate(IntPtr nativeTarget)  

任何人都可以告诉我它抱怨什么以及如何解决这个问题? 

Can anybody give me a clue as to what it is complaining about and how to get around this problem? 

我有点解决了这个问题。 事实证明,组合框甚至无法在SL4中正常工作。 它只是没有异常爆炸。 

I have kind of fixed the problem.  It turns out that the combobox wasn't even working right in SL4.  It just wasn't blowing up with an exception. 

但是我在SL 4中注意到第二个SubCategory组合框被支持选择了所选项目,但它显示了第一个项目作为所选项目。 当我弹出组合框选项时,ListBox DID会显示正确的选定项目(突出显示)。 
然后当我关闭列表框时,它现在显示了正确的选定项目。 我猜测所选项目的不匹配正在炸毁SL 5.

But I noticed in SL 4 that the Second SubCategory combobox, was SUPPOSED to have the selected item selected, but it SHOWED the first item as the selected item.  When I popped up the combobox choices, the ListBox DID show the right selected item (highlighted).  Then when I just CLOSED the Listbox, it now showed the proper selected item.  I'm guessing this mismatch of selected item is what was blowing up SL 5.

在页面加载期间,创建了视图模型,然后异步调用加载数据。 组合框以分层方式相关。 有3套组合框。 在第一个组合中第一个组合框中选择的值确定
第二个列表中的选项。 第二个列表控制第三个列表。 此外,它反向运作。 第三个控制第二个选项,这些选择控制第一个选择。

During the Page load, the viewmodel is created and then an async call loads the data.  The comboboxes are related in a hierarchical fashion.  There are 3 sets of comboboxes.  The value selected in the first combobox in the first set determines the choices in the second list.  And the second list controls the third list.  Additionally, it works in reverse.  The third controls the second's choices and those choices control the first choices.

因此,当设置了选择项时,所有这些列表都会在设置器中进行调整。

So, as the selecteditem is set, all these lists get adjusted in the setters.

我猜测更改选择内容以及设置其他组合框绑定属性的setter中所选项目是不喜欢的。

I'm guessing that something isn't liked about changing the choices and the selected item inside the setter that is setting other combobox bound properties.

所以我做的是,之后在Viewmodel初始化期间设置了这些组合框的所有内容,我保存所有列表和选定项目,将它们全部设置为null,然后将它们恢复为保存的值。 最后一次"摇晃"必须正确地触发
所有事件。

So what I did is, after EVERYTHING with these comboboxes is set during the Viewmodel initialization, I save all the lists and selected items, set them all to null, and then restore them to their saved values.  That last "jiggle" must properly trigger all the events properly.

我看不出有关分层内容的setter中的任何代码有什么问题,事实上所有的工作都很好在初始加载之后。

I couldn't see anything wrong with any of the code in the setters for the hierarchical stuff, in fact that all works just fine AFTER the initial loading.

所以问题得到解决虽然我不完全理解为什么SL 4做错了并且SL 5爆炸了。

So the problem is resolved though I don't totally understand why SL 4 did it wrong and SL 5 blew up.