RibbonComboBox将其DataContext更改为{DisconnectedItem}

时间:2013-01-09 22:29:28

标签: c# wpf ribbon

我正在开发一个WPF项目并正在尝试一种非常奇怪的行为。

我在功能区中添加了RibbonComboBox,其中有一个RibbonGallery,其RibbonGalleryCategory具有与元素数组的绑定。

<rb:RibbonComboBox Name="xComboBox" Label="List:" >
       <rb:RibbonGallery SelectedValue="{Binding SelectedValue, Mode=TwoWay}">
              <rb:RibbonGalleryCategory ItemsSource="{Binding List}" />
       </rb:RibbonGallery>
</rb:RibbonComboBox>

到目前为止,一切正常,当我运行程序时,RibbonComboBox的项目按预期运行。

当我将容器窗口调整到非常小的尺寸时,问题就出现了,在完成此操作并将其调整大小后,ComboBox 为空 !!

我不知道为什么会这样,我做错了什么?

我试图了解发生了什么,因此,我在Items的{​​{1}}属性中添加了一个事件,如下所示:

RibbonGalleryCategory

正如您所看到的,我展示了集合中的变化,因此,在运行程序并调整窗口大小后,我的小测试告诉我该集合是“重置”!!

有谁知道为什么会这样?如何防止RibbonComboBox丢失其数据?

提前谢谢。

修改

更多信息:我刚注意到一些事情,在调整容器窗口大小后,RibbonComboBox会为名为“ public RibbonView() { InitializeComponent(); RibbonGallery gallery = xComboBox.Items[0] as RibbonGallery; RibbonGalleryCategory galleryCat = gallery .Items[0] as RibbonGalleryCategory; ((INotifyCollectionChanged)galleryCat.Items).CollectionChanged += new NotifyCollectionChangedEventHandler(RibbonView_CollectionChanged); } void RibbonView_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { Dispatcher.BeginInvoke(new Action(() => { switch (e.Action) { case NotifyCollectionChangedAction.Add: MessageBox.Show("Collection has changed >>>> Add"); break; case NotifyCollectionChangedAction.Move: MessageBox.Show("Collection has changed >>>> Move"); break; case NotifyCollectionChangedAction.Remove: MessageBox.Show("Collection has changed >>>> Remove"); break; case NotifyCollectionChangedAction.Replace: MessageBox.Show("Collection has changed >>>> Replace"); break; case NotifyCollectionChangedAction.Reset: MessageBox.Show("Collection has changed >>>> Reset"); break; } }), System.Windows.Threading.DispatcherPriority.Background, null); } ”的对象更改其DataContext。我做了一些研究,发现this。但我仍然不知道如何防止它。

有人知道如何避免控件丢失其DataContext(这会使组合框丢失其数据)吗?

1 个答案:

答案 0 :(得分:1)

我意识到documentation提供的内容并不多,但请看Orion Edwards在this链接底部的回答。

基本上“发生了戏剧性的事情!”

也许正如他所建议的那样,从头开始重建列表,重新调整大小将成为诀窍。

相关问题