Same method for multiple controls when using Caliburn.Micro - wpf

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.

Related

How to connect ICommand and VisualStateManager together

How can I let my controls on the window know that their states should be changed. I have to use ICommand and since controls can have different states (Enable/Disable , Checked/Unchecked ...) have to handle them with VisualStateManager.
This could be a possible scenario:
When Record button is clicked(Checked) the other controls in the window should be uncheckable and once the recording is finished they should be checkable and many other scenarios.
Any help would be really appreciated.
Thanks.
You can achieve it with the help of interaction and interactivity. See if it helps.
Add below references in your XAML
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
You can bind your ICommand and provide a VisualState name that will be applied when Button is clicked
<Button>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding YourICommand}" />
<ei:GoToStateAction StateName="YourVisualStateName" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
If you don't have Blend 4, you can install the Blend 4 SDK to get the current behavior assemblies.

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>

WPF mvvm wizard

I am implementing WPF MVVM Wizard and I am wondering about the right approach of performing a DoOperation when a new Wizard’s Page (UserControl) is loaded.
The DoOperation is implemented on the MyWizard.ViewModal class while the UserControl Load is happening at the MyWizard.View namespaces.
How can I connect between the UserControl loaded event to the DoOperation api?
I tried the following:
<xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding Path=RunOperation}"/
</i:EventTrigger>
</i:Interaction.Triggers>
RunOperation calls DoOperation.
it doesn’t work, RunOperation is not being called.
This is the right approach or there is a better way to perform an operation at the MyWizard.ViewModal class?
Your approach should work. Have you checked your output console for binding errors? Is RunOperation a command? Is the DataContext of the UserControl already set, when the Loaded event is raised? Have you implemented the triggers like this in your UC?
<UserControl x:Class="..."
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding Path=RunOperation}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
...
</Grid>
</UserControl>

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.

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