3

I have a custom RibbonGallery control like in Excel, MSWord, and Outlook.

enter image description here

Please refer the Excel RibbonGallery image below and Normal selection still exists.

enter image description here

And I kept two ItemsSource, one for RibbonGallery View and one for Popup, and arranged the items in RibbonGallery and Popup View.

When I choose an item in RibbonGallery, the selection of SelectedItem (object) will be updated. If I open a popup, I cleared ItemsSource from the RibbonGallery (inorder to avoid the Element already added the child of another element issue) and reassigned it to the Popup ItemsControl. But the selection of the selected item is cleared after open/closing the popup.

 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 updated from ItemsControl Tapped event.

Anybody please tell me how to retain the selection when updating new collection to the control (RibbonGallery to popup and popup to RibbonGallery)?

Kanniyappan P
  • 281
  • 1
  • 6

1 Answers1

1

ItemsControl can't select items, only present collections. Only a Selector or one of it's descendants can select items. There is no concept of SelectedItem with an ItemsControl.

For Selector, the SelectedItem becomes null already after you clear the itemsource, such as listView, DataGrid. So you need to save the previous SelectedItem before you clear itemsource, then set the SelectedItem to previous SelectedItem.

dear_vv
  • 2,350
  • 1
  • 4
  • 13