I'm using Microsoft Expression Blend 4
I have a Browser ..,
[ XAML ] ConnectionView " Empty Code Behind "
<WebBrowser local:AttachedProperties.BrowserSource="{Binding Source}">
<i:Interaction.Triggers>
<i:EventTrigger>
<i:InvokeCommandAction Command="{Binding LoadedEvent}"/>
</i:EventTrigger>
<i:EventTrigger EventName="Navigated">
<i:InvokeCommandAction Command="{Binding NavigatedEvent}" CommandParameter="??????"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</WebBrowser>
[ C# ] AttachedProperties class
public static class AttachedProperties
{
public static readonly DependencyProperty BrowserSourceProperty = DependencyProperty . RegisterAttached ( "BrowserSource" , typeof ( string ) , typeof ( AttachedProperties ) , new UIPropertyMetadata ( null , BrowserSourcePropertyChanged ) );
public static string GetBrowserSource ( DependencyObject _DependencyObject )
{
return ( string ) _DependencyObject . GetValue ( BrowserSourceProperty );
}
public static void SetBrowserSource ( DependencyObject _DependencyObject , string Value )
{
_DependencyObject . SetValue ( BrowserSourceProperty , Value );
}
public static void BrowserSourcePropertyChanged ( DependencyObject _DependencyObject , DependencyPropertyChangedEventArgs _DependencyPropertyChangedEventArgs )
{
WebBrowser _WebBrowser = _DependencyObject as WebBrowser;
if ( _WebBrowser != null )
{
string URL = _DependencyPropertyChangedEventArgs . NewValue as string;
_WebBrowser . Source = URL != null ? new Uri ( URL ) : null;
}
}
}
[ C# ] ConnectionViewModel Class
public class ConnectionViewModel : ViewModelBase
{
public string Source
{
get { return Get<string> ( "Source" ); }
set { Set ( "Source" , value ); }
}
public void Execute_ExitCommand ( )
{
Application . Current . Shutdown ( );
}
public void Execute_LoadedEvent ( )
{
MessageBox . Show ( "___Execute_LoadedEvent___" );
Source = ...... ;
}
public void Execute_NavigatedEvent ( )
{
MessageBox . Show ( "___Execute_NavigatedEvent___" );
}
}
[ C# ] ViewModelBase class Here
Finally :
Binding with commands works well and MessageBoxes shown
My Question :
How to pass NavigationEventArgs as Command Parameters when Navigated Event occurs ?
To add to what joshb has stated already - this works just fine for me. Make sure to add references to Microsoft.Expression.Interactions.dll and System.Windows.Interactivity.dll and in your xaml do:
I ended up using something like this for my needs. This shows that you can also pass a custom parameter:
As an adaption of @Mike Fuchs answer, here's an even smaller solution. I'm using the
Fody.AutoDependencyPropertyMarker
to reduce some of the boiler plate.The Class
The EventArgs
The XAML
The ViewModel
I've always come back here for the answer so I wanted to make a short simple one to go to.
There are multiple ways of doing this:
1. Using WPF Tools. Easiest.
Add Namespaces:
System.Windows.Interactivitiy
Microsoft.Expression.Interactions
XAML:
Use the
EventName
to call the event you want then specify yourMethod
name in theMethodName
.Code:
2. Using MVVMLight. Most difficult.
Install GalaSoft NuGet package.
Get the namespaces:
System.Windows.Interactivity
GalaSoft.MvvmLight.Platform
XAML:
Use the
EventName
to call the event you want then specify yourCommand
name in your binding. If you want to pass the arguments of the method, markPassEventArgsToCommand
to true.Code Implementing Delegates: Source
You must get the Prism MVVM NuGet package for this.
Code Without
DelegateCommand
: Source3. Using Telerik EventToCommandBehavior. It's an option.
You'll have to download it's NuGet Package.
XAML
:Code:
For people just finding this post, you should know that in newer versions (not sure on the exact version since official docs are slim on this topic) the default behavior of the InvokeCommandAction, if no CommandParameter is specified, is to pass the args of the event it's attached to as the CommandParameter. So the originals poster's XAML could be simply written as:
Then in your command, you can accept a parameter of type
NavigationEventArgs
(or whatever event args type is appropriate) and it will automatically be provided.With Behaviors and Actions in Blend for Visual Studio 2013 you can use the InvokeCommandAction. I tried this with the Drop event and although no CommandParameter was specified in the XAML, to my surprise, the Execute Action parameter contained the DragEventArgs. I presume this would happen for other events but have not tested them.