How to get PivotItem from Button placed on PivotItemHeader

BitSmithy 2,206 Reputation points
2020-09-07T21:49:52.517+00:00

I build PivotItems with Close button on the PivotItemHeader:

                PivotItem pivi = new PivotItem();

                StackPanel headerStackPanel = new StackPanel();
                headerStackPanel.Orientation = Orientation.Horizontal;
                TextBlock tbx = new TextBlock();
                tbx.Text = e.ClickedItem.ToString();
                headerStackPanel.Children.Add(tbx);
                Button btn = new Button();
                btn.Click += btnClosePivotItem_Click;
                SymbolIcon si = new SymbolIcon(Symbol.Cancel);
                btn.Content = si;

                headerStackPanel.Children.Add(btn);

                pivi.Header = headerStackPanel;

Next, when I Click the Button I want to Close the PivotItem

How to get the PivotItem on Button_Click event?

    private void btnClosePivotItem_Click(object sender, RoutedEventArgs e)
    {

}

Developer technologies Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,866 Reputation points
    2020-09-08T02:08:44.343+00:00

    Hello
    Welcome to Microsoft Q&A,

    How to get PivotItem from Button placed on PivotItemHeader

    For this scenario, we suggest you use xaml binding to approach. If you want to remove item, you just need to process the data source, but not get the real control. Please refer following code.

    public sealed partial class MainPage : Page  
    {  
        public ObservableCollection<string> Items = new ObservableCollection<string>() {"One","Two","Three","Four" };  
        public MainPage()  
        {  
            this.InitializeComponent();  
            this.DataContext = this;  
            MyPivot.ItemsSource = Items;  
        }  
        public ICommand ItemCommand  
        {  
            get  
            {  
                return new CommadEventHandler<string>((item) => ItemClick(item));  
            }  
        }  
        private void ItemClick(string item)  
        {  
            Items.Remove(item);  
        }      
    }  
      
    class CommadEventHandler<T> : ICommand  
    {  
        public event EventHandler CanExecuteChanged;  
      
        public Action<T> action;  
        public bool CanExecute(object parameter)  
        {  
            return true;  
        }  
      
        public void Execute(object parameter)  
        {  
            this.action((T)parameter);  
        }  
        public CommadEventHandler(Action<T> action)  
        {  
            this.action = action;  
      
        }  
    }  
    

    Xaml

    23049-image.png

    I have make sample here.

    Thanks
    Nico Zhu

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.