在Silverlight中绑定ComboBox.SelectedItem(更多)

在Silverlight中绑定ComboBox.SelectedItem(更多),第1张

概述与我之前的问题相关: Binding ComboBox.SelectedItem in Silverlight 我有一个像这样绑定的ComboBox: <ComboBox x:Name="PART_CommentaryList" HorizontalAlignment="Left" Margin="3" ItemsSource="{Binding Path=Curre 与我之前的问题相关: Binding ComboBox.SelectedItem in Silverlight

我有一个像这样绑定的ComboBox:

<ComboBox x:name="PART_CommentaryList"     HorizontalAlignment="left"     margin="3"     ItemsSource="{Binding Path=CurrentVIDeo.CommentarIEs}"     SelectedItem="{Binding Path=CurrentCommentary,Mode=TwoWay}">

CurrentVIDeo和CurrentCommentary属性都会定期更改.几次后,我收到此错误:

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是否更改,因此所选项应该始终是列表中的项.

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

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

1)始终将ItemsSource绑定到可观察的集合,并且永远不会重置OC的指针.

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

坏:

MyList = new ObservableCollection();

好:

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

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

在您的情况下,只要更改CurrentVIEw,就会更改List的引用.因此,如果SelectedItem不为null,则重置ItemsSource的时间很短,ComboBox的内部正在尝试在新的ItemsSource中找到SelectedItem对象,但旧对象不在那里.

总结

以上是内存溢出为你收集整理的在Silverlight中绑定ComboBox.SelectedItem(更多)全部内容,希望文章能够帮你解决在Silverlight中绑定ComboBox.SelectedItem(更多)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1003501.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存