将 ItemsSource 更新到 ItemsControl

问题描述

我有一个自定义的 Ribbongallery 控件,例如 Excel、MSWord 和 Outlook。

enter image description here

请参考下面的 Excel Ribbongallery 图片正常选择仍然存在。

enter image description here

我保留了两个 ItemsSource,一个用于 Ribbongallery View,一个用于 Popup,并在 Ribbongallery 和 Popup View 中排列项目。

当我在 Ribbongallery 中选择一个项目时,SelectedItem(对象)的选择将被更新。如果我打开一个弹出窗口,我会从 Ribbongallery 中清除 ItemsSource(为了避免 Element 已经添加了另一个元素问题的子项)并将其重新分配给 Popup ItemsControl。但是在打开/关闭弹出窗口后,所选项目的选择被清除。

 private void UpdateItemsSource()
    {
        if (!this.IsDropDownopen)
        {
            this.popupgalleryItemsControl.ItemsSource = null;
            this.ribbongalleryItemsControl.ItemsSource = this.ItemsSource;
        }
        else
        {
            this.ribbongalleryItemsControl.ItemsSource = null;
            this.popupgalleryItemsControl.ItemsSource = this.ItemsSource;
        }
    }



    <ItemsControl x:Name="RibbongalleryItemsControl"
                                 ItemContainerStyle="{TemplateBinding ItemContainerStyle}"
                                 ItemTemplate="{TemplateBinding ItemTemplate}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <ItemsWrapGrid Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

<ItemsControl x:Name="PopupItemsControl"
                             ItemContainerStyle="{TemplateBinding ItemContainerStyle}"
                             ItemTemplate="{TemplateBinding ItemTemplate}">
<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <ItemsWrapGrid Orientation="Horizontal" />
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

SelectedItemItemsControl Tapped 事件更新。

有人请告诉我在将新集合更新到控件时如何保留选择(Ribbongallery 到 popup 和 popup 到 Ribbongallery)?

解决方法

ItemsControl 不能选择项目,只能显示集合。只有 Selector 或其后代之一可以选择项目。没有带有 ItemsControl 的 SelectedItem 概念。

对于Selector,当你清除itemsource,比如listView,DataGrid,SelectedItem就已经为null了。所以你需要在清除itemsource之前保存之前的SelectedItem,然后将SelectedItem设置为之前的SelectedItem。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...