在Silverlight中绑定ComboBox.SelectedItem(更多) [英] Binding ComboBox.SelectedItem in Silverlight (more)

查看:172
本文介绍了在Silverlight中绑定ComboBox.SelectedItem(更多)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与我上一个问题相关:在Silverlight中绑定ComboBox.SelectedItem

Related to my previous question: Binding ComboBox.SelectedItem in Silverlight

我有一个ComboBox绑定像这样:

I have a ComboBox bound like so:

<ComboBox x:Name="PART_CommentaryList" 
    HorizontalAlignment="Left" 
    Margin="3" 
    ItemsSource="{Binding Path=CurrentVideo.Commentaries}" 
    SelectedItem="{Binding Path=CurrentCommentary, Mode=TwoWay}">

CurrentVideo和CurrentCommentary属性定期更改。几次后,我收到此错误:

Both the CurrentVideo and CurrentCommentary property change regularly. After a few times, I get this error:

Category: ManagedRuntimeError       
Message: System.ArgumentException: Value does not fall within the expected
   range.
   at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, 
       CValue[] cvData)
   at MS.Internal.XcpImports.MethodPack(IntPtr objectPtr, String methodName, 
       Object[] rawData)
   at MS.Internal.XcpImports.UIElement_TransformToVisual(UIElement element, 
       UIElement visual)
   at System.Windows.UIElement.TransformToVisual(UIElement visual)
   at System.Windows.Controls.Primitives.Selector.IsOnCurrentPage(
       Int32 index, Rect& itemsHostRect, Rect& listBoxItemRect)
   at System.Windows.Controls.Primitives.Selector.ScrollIntoView(
       Int32 index)
   at System.Windows.Controls.Primitives.Selector.SetFocusedItem(
       Int32 index, Boolean scrollIntoView)
   at System.Windows.Controls.ComboBox.PrepareContainerForItemOverride(
       DependencyObject element, Object item)
   at System.Windows.Controls.ItemsControl.UpdateContainerForItem(
       Int32 index)
   at System.Windows.Controls.ItemsControl.RecreateVisualChildren()
   at System.Windows.Controls.ItemsControl.RecreateVisualChildren(
       IntPtr unmanagedObj)

这看起来像是一个ComboBox错误。我可以在CurrentCommentary之前验证CurrentVideo的更改,所以选择的项目应该总是一个列表中的项目。

This seems like a ComboBox bug to me. I can verify that CurrentVideo changes before CurrentCommentary, so the selected item should always be an item which is in the list.

相关,我真的不想要Mode = TwoWay,因为当ItemsSource被改变时,SelectedItem暂时为null,它在我的模型中被设置回来,这实际上我不想要。但是绑定在其他方面都不起作用(这似乎是另一个错误)。

Related, I really don't want the Mode=TwoWay, because when the ItemsSource is changed, the SelectedItem is temporarily null, which gets set back in my model, which I don't actually want. But the binding doesn't work at all otherwise (which seems like another bug).

推荐答案

这是ComboBox中的一个错误控件与ItemsSource的绑定的更改指针有关。我找到的解决方案是:

This is a bug in the ComboBox control that has to do with the changing pointer of the ItemsSource's binding. The solution that I have found is to:

1)始终将ItemsSource绑定到一个可观察的集合,从不重置OC的指针。

1) Always bind the ItemsSource to an observable collection and never reset the pointer of the OC.

<ComboBox ItemsSource="{Binding MyList}" SelectedItem="{Binding MyItem}" />

错误:

MyList = new ObservableCollection();

好:

MyList.Clear();
MyList.AddRange(...);

2)在清除MyList之前设置MyItem = null

2) Set MyItem = null, before Clearing MyList

在你的情况下,只要你改变CurrentView,你就改变列表的引用。因此,如果SelectedItem不为null,那么在ItemsSource被重置的时候,ComboBox的内部将尝试在新的ItemsSource中找到SelectedItem对象,但是旧的对象不存在。

In your case you are changing the reference of the List whenever you change CurrentView. Therefore, if SelectedItem is not null, there is a brief moment in time where the ItemsSource is being reset, the internals of the ComboBox are attempting to locate the SelectedItem object in the new ItemsSource but the old object is not there.

这篇关于在Silverlight中绑定ComboBox.SelectedItem(更多)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆