与我之前的问题相关:在 Silverlight 中绑定 ComboBox.SelectedItem
我有一个像这样绑定的 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 错误。我可以验证 CurrentVideo 在 CurrentCommentary 之前更改,因此所选项目应始终是列表中的项目。
相关,我真的不想要 Mode=TwoWay,因为当 ItemsSource 更改时,SelectedItem 暂时为空,这会在我的模型中重新设置,而我实际上并不想要。但是绑定根本不起作用(这似乎是另一个错误)。