Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# RibbonComboBox将其DataContext更改为{DisconnectedItem}_C#_Wpf_Ribbon - Fatal编程技术网

C# RibbonComboBox将其DataContext更改为{DisconnectedItem}

C# RibbonComboBox将其DataContext更改为{DisconnectedItem},c#,wpf,ribbon,C#,Wpf,Ribbon,我正在开发一个WPF项目,正在试验一种非常奇怪的行为 我在功能区中添加了一个RibbonComboBox,其中有一个RibbonGallery,它具有一个绑定到元素数组的RibbonGallery类别 <rb:RibbonComboBox Name="xComboBox" Label="List:" > <rb:RibbonGallery SelectedValue="{Binding SelectedValue, Mode=TwoWay}">

我正在开发一个WPF项目,正在试验一种非常奇怪的行为

我在功能区中添加了一个
RibbonComboBox
,其中有一个
RibbonGallery
,它具有一个绑定到元素数组的
RibbonGallery类别

<rb:RibbonComboBox Name="xComboBox" Label="List:" >
       <rb:RibbonGallery SelectedValue="{Binding SelectedValue, Mode=TwoWay}">
              <rb:RibbonGalleryCategory ItemsSource="{Binding List}" />
       </rb:RibbonGallery>
</rb:RibbonComboBox>
如您所见,我显示了集合中的更改,因此,在运行程序并调整窗口大小后,我的小测试告诉我集合被“重置”

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

先谢谢你

编辑:

更多信息:我刚刚注意到,在调整容器窗口的大小后,RibbonComboBox更改了名为“
{DisconnectedItem}
”的对象的DataContext。我做了一些研究发现。但我仍然不知道如何预防它

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

我意识到这没有什么可以提供的,但请参见链接底部的Orion Edwards答案

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

正如他所建议的那样,重新确定名单的大小可能是一个技巧。

我意识到这没有什么可以提供的,但请看猎户座爱德华兹在链接底部的回答

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

也许,正如他所建议的那样,重新确定名单的大小后,从头开始重建名单将是一个诀窍

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);
    }