Category / Section
How to Invoke the command on clicking the SfTreeNavigatorItems
1 min read
To invoke the command on clicking the item, SelectionChanged event of TreeViewItem can be used. The same has been explained it in the following code example:
XAML:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" <Grid> <navigation:SfTreeNavigator ItemsSource="{Binding Models}" Header="Enterprise Toolkit" Width="300" Height="400" HorizontalAlignment="Center" VerticalAlignment="Center"> <!--Invoke the command using EventToCommand--> <i:Interaction.Behaviors> <local:EventToCommand Command="{Binding DataContext.Clicked, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" EventName="SelectionChanged"/> </i:Interaction.Behaviors> <navigation:SfTreeNavigator.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Models}"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Header}" Foreground="Green" FontWeight="Bold" VerticalAlignment="Center" Margin="18 0 0 0"/> </StackPanel> </HierarchicalDataTemplate> </navigation:SfTreeNavigator.ItemTemplate> </navigation:SfTreeNavigator> </Grid>
C#: [EventtoCommand class which helps to invoke the command]
public class EventToCommand : Behavior<FrameworkElement> { //variables private Delegate _handler; private EventInfo _oldEvent; // Properties public string EventName { get { return (string)GetValue(EventProperty); } set { SetValue(EventProperty, value); } } public static readonly DependencyProperty EventProperty = DependencyProperty.Register("EventName", typeof(string), typeof(EventToCommand), new PropertyMetadata(null, OnEventChanged)); //Commands public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } //Dependency property public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(EventToCommand), new PropertyMetadata(null)); // Method to invoke on event changed private static void OnEventChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var eventdata = (EventToCommand)d; if (eventdata.AssociatedObject != null) eventdata.AttachHandler((string)e.NewValue); } protected override void OnAttached() { AttachHandler(this.EventName); } private void AttachHandler(string eventName) { if (_oldEvent != null) _oldEvent.RemoveEventHandler(this.AssociatedObject, _handler); if (!string.IsNullOrEmpty(eventName)) { //Getting the type of the control SfTreeNavigator item = this.AssociatedObject as SfTreeNavigator; EventInfo ei = null; if (item != null) { ei = item.GetType().GetEvent(eventName); } if (ei != null) { MethodInfo mi = this.GetType().GetMethod("ExecuteCommand", BindingFlags.Instance | BindingFlags.NonPublic); _handler = Delegate.CreateDelegate(ei.EventHandlerType, this, mi); ei.AddEventHandler(item, _handler); _oldEvent = ei; } } } //Method to invoke the commands private void ExecuteCommand(object sender, EventArgs e) { object[] parameter = null; if (parameter == null) { parameter = new object[2]; parameter[0] = sender; parameter[1] = e; } if (this.Command != null) { if (this.Command.CanExecute(parameter)) this.Command.Execute(parameter); } } }
MainViewModel class
public class TreeViewModel { public TreeViewModel() { //Intializing the command Clicked = new DelegateCommand<object[]>(executeCommandOne); } //Implementing the method to invoke the command private void executeCommandOne(object[] obj) { MessageBox.Show("Command has been invoked"); } #endregion // Declaring the DelegateCommand public DelegateCommand<object[]> Clicked { get; set; } }
Screenshot:
Fig i: Screenshot shows the command has been invoked in SfTreeNavigator