Search code examples
c#xamluwpuno-platform

Add custom user control on pivot selection change


I have Pivot with a custom user control in it.

 <Pivot.ItemTemplate>
                <DataTemplate>
                    <local1:mycustomusercontrol></local1:mycustomusercontrol>
                </DataTemplate>
  </Pivot.ItemTemplate>

Now on the selected item change on Pivot I want my mycustomusercontrol to be replaced with another user control. Is there a way to achieve this?

  private void PivotContainer_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (PivotContainer.SelectedIndex != 0)
        {
           //Change user control
        }
    }

Solution

  • UIElement has the property Children which contains all the child elements it contains, you could perhaps try to change that.

    However, personally I would prefer to use a DataTemplateSelector and inside of that define the logic of which DataTemplate to show based on some criteria on the ViewModel displayed on that position.

    Alternatively if DataTemplateSelector doesn't do it for you, you could define both states in your mycustomusercontrol and toggle visibility for them.

    Lots of solutions to your problem.