WPF: How to bind a command to the ListBoxItem using MVVM?

Unfortunately, only ButtonBase derived controls have the possibility for binding ICommand objects to their Command properties (for the Click event). However, you can use an API provided by Blend to map an event (like in your case MouseDoubleClick on the ListBox) to an ICommand object. <ListBox> <i:Interaction.Triggers> <i:EventTrigger EventName=”MouseDoubleClick”> <i:InvokeCommandAction Command=”{Binding YourCommand}”/> </i:EventTrigger> </i:Interaction.Triggers> </ListBox> … Read more

WPF/MVVM – how to handle double-click on TreeViewItems in the ViewModel?

Updating my answer a bit. I’ve tried alot of different approaches for this and I still feel like Attached Behaviors is the best solution. Although it might look like alot of overhead in the begining it really isn’t. I keep all of my behaviors for ICommands in the same place and whenever I need support … Read more

Bind command to Loaded event of view

If you want to bind command to the Loaded event, you should use the “System.Windows.Interactivity” assembly. <UserControl x:Class=”Components.Map.MapView” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” xmlns:map=”clr-namespace:Components.Map” xmlns:controls=”clr-namespace:Windows.Controls;assembly=Windows.Controls” xmlns:ValidationRules=”clr-namespace:Windows.Controls.ValidationRules;assembly=Windows.Controls” xmlns:directGraphicsControl=”clr-namespace:Windows.DirectGraphicsControl;assembly=Windows.DirectGraphicsControl” xmlns:colorBar=”clr-namespace:Components.Common.ColorBar;assembly=Components.Common” xmlns:RefinedRibbonControls=”clr-namespace:Components.Common.Controls.RefinedRibbonControls;assembly=Components.Common” xmlns:UserControls=”clr-namespace:Components.Common.UserControls;assembly=Components.Common” xmlns:map1=”clr-namespace:Models.Map;assembly=Models.Map” xmlns:utilities=”clr-namespace:Windows.Utilities;assembly=Windows.Utilities” xmlns:system=”clr-namespace:System;assembly=mscorlib” xmlns:i=”clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity”> <i:Interaction.Triggers> <i:EventTrigger EventName=”Loaded”> <i:InvokeCommandAction Command=”{Binding LoadedCommand}” /> </i:EventTrigger> </i:Interaction.Triggers> </UserControl> System.Windows.Interactivity.dll is in Microsoft Expression Blend Software Development Kit (SDK) (download link) and also … Read more

WPF DataGrid: CommandBinding to a double click instead of using Events

No need for attached behaviors or custom DataGrid subclasses here. In your DataGrid, bind ItemsSource to an ICollectionView. The trick here is to set IsSynchronizedWithCurrentItem=”True” which means the selected row will be the current item. The second part of the trick is to bind CommandParameter to the current item with the forward slash syntax. When … Read more

How to bind Close command to a button

All it takes is a bit of XAML… <Window x:Class=”WCSamples.Window1″ xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”> <Window.CommandBindings> <CommandBinding Command=”ApplicationCommands.Close” Executed=”CloseCommandHandler”/> </Window.CommandBindings> <StackPanel Name=”MainStackPanel”> <Button Command=”ApplicationCommands.Close” Content=”Close Window” /> </StackPanel> </Window> And a bit of C#… private void CloseCommandHandler(object sender, ExecutedRoutedEventArgs e) { this.Close(); } (adapted from this MSDN article)