how to implement control events in wpf using MVVM? - wpf

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>

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 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

DataGrid event handling in MVVM

I have a DataGrid and need to handle its events. I have a business logic to be implemented which needs to be handled in the ViewModel and able to unit test.
Can I raise events and handle them in the ViewModel? In this case how to unit test?
You can use EventTrigger from System.Windows.Interactivity or MvvmLight
<DataGrid x:Name="myProtokollList">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding Path=OpenCommand}" CommandParameter="{Binding ElementName=myProtokollList, Path=SelectedItem.OriginalSatzX}"/>
</i:EventTrigger>
</i:Interaction.Triggers>

Attaching Command to ScrollViewer.ScrollChanged of ListView

The WPF ListView class can be set up to automatically handle scrolling without an external ScrollViewer and it's possible to register an event handler for the control's internal scrollbar by writing XAML like such:
<ListView ScrollViewer.ScrollChanged="ScrollChanged" />
How to attach it to MVVM light Command? I tried the following, but it doesn't work:
<ListView>
<i:Interaction.Triggers>
<i:EventTrigger EventName="ScrollViewer.ScrollChangedEvent">
<cmd:EventToCommand Command="{Binding ScrollCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ListView>
Note: ScrollCommand - is a RelayCommand from my viewmodel.
EventTrigger doesn't trigger for routed events. You can use the solution proposed in this article
to create a RoutedEventTrigger class and use it instead of EventTrigger.
I recommend a Behavior for this. If you don't have Blend, you'll need to get the Blend SDK. But once you have that, you can follow this tutorial to extend the behavior of the ScrollViewer.

Bind a ICommand to a WPF tabcontrol/tabitem using XAML (MVVM)

I have a WPF 3.5 app built using MVVM pattern. I have a tabcontrol and I want to excecute an ICommand on the view model when the user clicks a certain tab (jn my case the "Preview" tab).
How do I hook up my ICommand to the tabitem or tabcontrol using just the XAML? I am using MVVM so I dont want to use events and get filthy, dirty code in the code-behind.
I think Im missing something simple here!
Thanks,P
Please refer this article : hooking up commands to events
Or you can use this approach
You should use InvokeCommandAction, Looks like this:
<TabItem Header="TabItem">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction Command="SomeCommand"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid />
</TabItem>
Be sure to include the xmlns in the top:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
and of course to reference System.Windows.Interactivity (new in WPF4)

Resources