Category / Section
How to invoke the command for groupbar item in WPF GroupBar control?
2 mins read
You can invoke the command for groupbar item by using MouseDown event of WPF GroupBar and bound the event using the interaction behavior. Refer the below code for your reference.
XAML
<syncfusion:GroupBar ItemsSource="{Binding GroupItemCollection}" VisualMode="StackMode"> <syncfusion:GroupBar.ItemContainerStyle> <Style TargetType="{x:Type syncfusion:GroupBarItem}" > <Setter Property="Header" Value="{Binding Header}"/> <Setter Property="Content" Value="{Binding Content}"/> <Setter Property="IsSelected" Value="{Binding IsSelected}" /> </Style> </syncfusion:GroupBar.ItemContainerStyle> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseDown"> <command:EventToCommand Command="{Binding MouseDownClick}" CommandParameter="{Binding InvokeParameter}" PassEventArgsToCommand="True"/> </i:EventTrigger> </i:Interaction.Triggers> </syncfusion:GroupBar>
ViewModel.cs
public class ViewModel { private ICommand mouseDownClick; public ICommand MouseDownClick { get { return mouseDownClick; } set { mouseDownClick = value; } } private ObservableCollection<Model> groupItemCollection; public ObservableCollection<Model> GroupItemCollection { get { return groupItemCollection; } set { groupItemCollection = value; } } public ViewModel() { GroupItemCollection = new ObservableCollection<Model>(); GroupItemCollection.Add(new Model() { Header = "GroupBarItem1", Content = "This is GroupBarItem", IsSelected = true }); GroupItemCollection.Add(new Model() { Header = "GroupBarItem2", Content = "This is GroupBarItem1", IsSelected = true }); GroupItemCollection.Add(new Model() { Header = "GroupBarItem3", Content = "This is GroupBarItem2", IsSelected = false }); GroupItemCollection.Add(new Model() { Header = "GroupBarItem4", Content = "This is GroupBarItem3", IsSelected = true }); GroupItemCollection.Add(new Model() { Header = "GroupBarItem5", Content = "This is GroupBarItem4", IsSelected = true }); MouseDownClick = new DelegateCommand<object>(MouseDownClicked); } private void MouseDownClicked(object obj) { MessageBox.Show("Mouse down event called "); } }
Model.cs
public class Model : NotificationObject { private string header; public string Header { get { return header; } set { header = value; } } private string content; public string Content { get { return content; } set { content = value; } } private bool isselected; public bool IsSelected { get { return isselected; } set { isselected = value; } } private int invokeParameter = 1; public int InvokeParameter { get { return invokeParameter; } set { invokeParameter = value; RaisePropertyChanged("InvokeParameter"); } } }
Output:
Sample: View sample in GitHub.