Articles in this section
Category / Section

How to add custom context menu in TileViewItem

1 min read

We can add custom context menu to the item of TileViewControl. The following code example explains the same,

 

Adding Custom ContextMenu to the TileViewItem :

<syncfusion:TileViewItem.ContextMenu>
    <ContextMenu>
        <MenuItem />
        <MenuItem />
    </ContextMenu>
</syncfusion:TileViewItem.ContextMenu>

 

For instance, we have added new TileViewItem to the TileViewControl and removed existing item to the TileViewControl through the Custom context menu. The following code example demonstrate the same,

Mainwindow.xaml :

       <syncfusion:TileViewControl Name="tileViewControl1" >
            <syncfusion:TileViewItem x:Name="item1" Header="Item 1" >
                <syncfusion:TileViewItem.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Add" Command="{Binding AddCommand}"/>
                        <MenuItem Header="Remove" Command="{Binding RemoveCommand}"/>
                    </ContextMenu>
                </syncfusion:TileViewItem.ContextMenu>
            </syncfusion:TileViewItem>
            <syncfusion:TileViewItem Header="Item 2"/>
            <syncfusion:TileViewItem Header="Item 3"/>
            <syncfusion:TileViewItem Header="Item 4" />
        </syncfusion:TileViewControl>

 

Mainwindow.cs :

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }
 
        private ICommand addCommand;
        public ICommand AddCommand
        {
            get
            {
                if (addCommand == null)
                    addCommand = new DelegateCommand<object>(param => AddItem(param), CanAdd);
                return addCommand; 
            }
            set { addCommand = value; }
        }
 
        private ICommand removeCommand;
        public ICommand RemoveCommand
        {
            get
            {
                if (removeCommand == null)
                    removeCommand = new DelegateCommand<object>(param => RemoveItem(param), CanRemove);
                return removeCommand;
            }
            set { removeCommand = value; }
        }
 
        private void RemoveItem(object window)
        {
            int count = this.tileViewControl1.Items.Count - 1;
            TileViewItem item = tileViewControl1.Items[count] as TileViewItem;
            this.tileViewControl1.Items.Remove(item);
        }
 
        private bool CanRemove(object obj)
        {
            return true ;
        }
 
        private bool CanAdd(object param)
        {
            return true;
        }
        private void AddItem(object param)
        {
            
            int count = this.tileViewControl1.Items.Count + 1;
            this.tileViewControl1.Items.Add(new TileViewItem(){Header = " Item "+count+""});
        }
    }

 

Screenshot :

Screenshot

Sample :

https://www.syncfusion.com/downloads/support/directtrac/general/ze/TileViewItem_ContextMenu335168137.zip

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied