An error in handling event. MVVM - wpf

I am using Visual Studio 2010 SP1. I've met a lot of articles where describes code of event handling(see below). For example this and this one.
I've added an assembly System.Windows.Interactivity. I am using ICommand interface to handle Commands. However, when I write a property "Command" in the line:
<i:InvokeCommandAction Command="{Binding SelectionChangedCommand}"/>
I catch an error: Property "Command" does not exist in namespace XML "clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"..
<ListBox ItemsSource="{Binding SomeData}" Grid.Row="2" Margin="10" Background="LightGreen">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<i:InvokeCommandAction CommandName="{Binding SelectionChangedCommand}" CommandParameter="{Binding YourCommandParameter}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
Thus I have two questions:
Where can I get a property Command? Or what should I write instead of it? I've tried to write CommandName but I've caught an error.
What should I write to CommandParameter property? What type of a property should I create in a viewmodel to bind it to CommandParameter?

As we talked in the WPF chat room, take a look in the following question, it is quite similar to your problem.
Properties does not exist : System.Windows.Interactivity?

Related

MVVM Light, Mahapps metro and EventToCommand via EventTrigger from System.Windows.Interactivity

I'm going nuts on this one:
I have an application using both Mahapps.Metro as well as MVVMLight.
Basically most things are ok UNTIL you try to use Interaction.Triggers.
I typically end up with errors like
Cannot add instance of type 'EventToCommand' to a collection of type 'TriggerActionCollection'. Only items of type 'T' are allowed.
A way to reproduce this is via a repo I found online:
https://github.com/mike-schulze/mahapps-mvvmlight-setup
(sorry mike for hijacking) and adding a metro SplitButton to the viewmodel:
<Controls:SplitButton ItemsSource="{Binding MyList}" Grid.Column="1" Grid.Row="1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand
Command="{Binding Mode=TwoWay, Path=SelectionChangedCommand}"
PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Controls:SplitButton>
And MyList being
public List<string> MyList
{
get
{
List<string> theList = new List<string>();
theList.Add("Hello");
theList.Add("World");
return theList;
}
}
I'm not sure how many different namespaces, hard coded Interactivity versions in App.config or what so ever I tried.
Has anyone a working example with Mahapps Metro (version not too old) and MVVM Light and the EventToCommand stuff?
I thought my problem is related to updates to Metro - and them using Behaviors now instead of Interactivity. I'm using the Interactivity dll version 4.5.0.0.
But even the old git I mentioned above shows the problem... And I can't find a working solution.
Any help is greatly appreciated.
Thanks
You need to add the namespace for System.Windows.Interactivity too.
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Because MVVMLight uses this version of Interactivity.
<mah:MetroWindow x:Class="..."
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
...
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:command="http://www.galasoft.ch/mvvmlight"
...
DataContext="{Binding Main, Source={StaticResource Locator}}">
...
<mah:SplitButton ItemsSource="{Binding MyList}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<command:EventToCommand Command="{Binding Mode=TwoWay, Path=SelectionChangedCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</mah:SplitButton>
...
</mah:MetroWindow>

How can I use MVVM light to execute a Command on a text box text change event

I have installed MVVMLIGHT but still somehow I can't access EventToCommand property even after adding the reference files and using the namespaces in xaml file
xmlns:i="http://schemas.microsoft.com expression/2010/interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight;assembly=GalaSoft.MvvmLight"
<Textbox x:name="quantity">
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
//not working <cmd:EventToCommand Command="{Binding quantityCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Textbox>
Even InvokeCommandAction would work but I m not getting that too.Plz show me.thank you in advance

Event trigger is not working inside ItemControl

I have a Item control which fills by a list, and list is collection of two parameters 'Time' and 'Description'. For it, i am using a HyperLinkButton for time and a Label for description.
What i want is that, i want to create click event using EventTrigger of hyperLink button in Main viewModel. My code is:
<ItemsControl
x:Name="transcriptionTextControl"
ItemsSource="{Binding MyCollectionOfTranscription, Mode=TwoWay}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<HyperlinkButton Content="{Binding Time}">
<ToolTipService.ToolTip>
<ToolTip Content="Time"/>
</ToolTipService.ToolTip>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction
Command="{Binding HyperLinkButtonCommand}"
CommandParameter="{Binding
ElementName=transcriptionTextControl }" />
</i:EventTrigger>
</i:Interaction.Triggers>
</HyperlinkButton>
<sdk:Label Content="{Binding Description}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
When i build project, it doesn't give error but ICommand for hyperLink, shows warning as 'Cannot resolve symbol HyperLinkButtonCommand', while this event trigger is working fine outside this .
Not getting, what is actual problem behind it, plz give your valuable suggestion...
First off,
<i:InvokeCommandAction
Command="{Binding HyperLinkButtonCommand}"
CommandParameter="{Binding
ElementName=transcriptionTextControl }" />
The Binding is trying to locate a property called HyperLinkButtonCommand on the instance of the type that is contained within MyCollectionOfTranscription (you don't need to bind to this two-way).
(Side note, sending an ItemsControl into your Command is not MVVM.)
The ItemsControl iterates through each element in this collection, creates a copy of the template defined in ItemsControl.ItemTemplate, and sets the BindingContext equal to this element (I assume its a Transcript). You can tell this from the warnings you get from the binding failing to find your HyperLinkButtonCommand if you crank up databinding debug settings.
Assuming
HyperLinkButtonCommand is a command defined in your ViewModel, and
The root of this xaml is a Window (could be a UserControl, but am assuming here)
Your ViewModel is the DataContext of the Window
you can change the binding to the following and it should work (or you should get a clue from it)
<i:InvokeCommandAction
Command="{Binding HyperLinkButtonCommand,
RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
CommandParameter="{Binding
ElementName=transcriptionTextControl }" />
I prefer just to give my root an x:Name of "root" and use "ElementName=root" in cases like this.

MVVM interaction drop trigger

I have a ListView that I need to function as a drop target. I have added the following trigger
<i:Interaction.Triggers>
<i:EventTrigger EventName="Drop">
<i:InvokeCommandAction Command="{Binding ItemsDroppedCommand}"
CommandParameter="{Binding ???}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
The problem is though I dont know how to get the dropped items. What should go in the CommandParameter binding?
If I do a drop handler in code behind I get a DragEventArgs parameter that enables me to get the files dropped. Is there a way to get this?
If this is the wrong approach please feel free to suggest alternatives
Passing an event's arguments to a Command through binding isn't supported out of the box but can be achieved through a workaround.
However, I would recommend you to use the EventToCommand behavior available in MVVM Light, which enables exactly this scenario:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Drop">
<cmd:EventToCommand Command="{Binding ItemsDroppedCommand}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
please take a look on this thread MVVM Passing EventArgs As Command Parameter
In this thread will help you solve the problem.
I hope this help.

Convert the RoutedEvent to the command for MVVM pattern in WPF

How to convert the Checkbox.checked routed event to the command for MVVM pattern?
Reference-Consider the button click routed event, When we assign the command for button then button.click event is considered as command. It executes respective that command.
I googled for it, i got the solution with EventBehaviourFactory. Is it possible without using the EventBehaiourFactory?
It is a common requirement in WPF. The most widely used solution is using Interactivity from the Blend SDK:
Add this xmlns namespace to your xaml:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
Then you can do:
<CheckBox>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding MyCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
Of course, you need to add System.Windows.Interactivity.dll to your project, located (for me at least) at: C:\Program Files (x86)\Microsoft SDKs\Expression\Blend 3\Interactivity\Libraries\WPF\System.Windows.Interactivity.dll

Resources