MVVM interaction drop trigger - wpf

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.

Related

Same method for multiple controls when using Caliburn.Micro

In WPF MVVM application, I need same functionality for multiple controls - for example certain button does same thing as certain menu item. It is piece of cake with MVVM Light's RelayCommand, but I am now using Caliburn.Micro, where almost everything is based on conventions. So two controls can not have same x:Name="AddItem", which is used by CM to determine method for executing in ViewModel. Is there any simple way to solve this?
Yes, it's simple, but verbose. You need to use the "long format". Let's say you have one method IncrementCount on your ViewModel:
// Handling event
public void IncrementCount()
{
Count++;
}
And your View has:
<Button Name="ButtonOne">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cal:ActionMessage MethodName="IncrementCount" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button Name="ButtonTwo">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cal:ActionMessage MethodName="IncrementCount" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
Both buttons will call your IncrementCount method.
EDIT
Add these namespaces
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cal="http://www.caliburnproject.org"
You may see this Caliburn starting project using the snippets above.

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

An error in handling event. MVVM

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?

How to use Interaction Trigger on button with MouseUp and MouseDown events in XAML?

I have a button that needs to execute two separate commands (one to start something and one to stop it). After doing some research the System.Windows.Interactivity.dll seemed to provide an easy way to accomplish this. But it doesn't work with the left mouse button (it does work if I use an event like MouseDoubleClick or MouseRightButtonDown, but not MouseDown, MouseUp, or MouseRightButtonDown)...it seems as if the button consumes the event itself and the interaction.trigger never sees it. I provided a snippet of my XAML below, what can I do to get around this behavior?
<Button Content="DoStuff">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<i:InvokeCommandAction Command="{Binding StartCommand}" />
</i:EventTrigger>
<i:EventTrigger EventName="MouseUp">
<i:InvokeCommandAction Command="{Binding StopCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
You can use PreviewMouseDown and PreviewMouseUp instead
mouseleftbuttondown , mouseleftbuttonup

how to implement control events in wpf using MVVM?

I have a slider control in view i need to call the ValueChanged event of slider and update one property in model based on that. how i can implement this using MVVM pattern.
where I have to write the ValueChanged event? how i can connect ValueChanged event code with view?
i just take the answer from here cause i use it in my projects too:
You should use an EventTrigger in combination with InvokeCommandAction from the Windows.Interactivity namespace. Here is an example:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelectedItemChangedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>

Resources