如何解决将 ItemsSource 更新到 ItemsControl
我有一个自定义的 RibbonGallery 控件,例如 Excel、MSWord 和 Outlook。
请参考下面的 Excel RibbonGallery 图片,正常选择仍然存在。
我保留了两个 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>
SelectedItem 从 ItemsControl Tapped 事件更新。
有人请告诉我在将新集合更新到控件时如何保留选择(RibbonGallery 到 popup 和 popup 到 RibbonGallery)?
解决方法
ItemsControl 不能选择项目,只能显示集合。只有 Selector 或其后代之一可以选择项目。没有带有 ItemsControl 的 SelectedItem 概念。
对于Selector,当你清除itemsource,比如listView,DataGrid,SelectedItem就已经为null了。所以你需要在清除itemsource之前保存之前的SelectedItem,然后将SelectedItem设置为之前的SelectedItem。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。