【发布时间】:2013-01-09 22:29:27
【问题描述】:
我正在开发一个 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 empty!!
我不知道为什么会这样,我做错了什么吗??
我试图查看发生了什么,因此,我在RibbonGalleryCategory 的Items 属性中添加了一个事件,如下所示:
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);
}
如你所见,我展示了集合的变化,所以,在运行程序并调整窗口大小后,我的小测试告诉我集合是“reset”!
有人知道为什么会这样吗?如何防止 RibbonComboBox 丢失数据?
提前谢谢你。
编辑:
更多信息:我刚刚注意到,在调整容器窗口大小后,RibbonComboBox 更改了名为“{DisconnectedItem}”的对象的 DataContext。我做了一些研究,发现this。但是我还是不知道怎么预防。
有谁知道如何避免控件丢失其 DataContext(这会使组合框丢失其数据)?
【问题讨论】: