I've encountered a problem with events using WpfAnimatedGif NuGet package. Package has two additional events for <image> but I'm unable to use them with MVVM pattern. I've tried interactivity:
<Window
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:gif="http://wpfanimatedgif.codeplex.com">
<Image gif:ImageBehavior.AnimatedSource="animation.gif">
<i:Interaction.Triggers>
<i:EventTrigger EventName="AnimationCompleted">
<i:InvokeCommandAction Command="{Binding SomeCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Image>
</Window>
but it fails, command doesn't get called. I've messed around a bit and got it kinda working (not realy). If I leave EventTrigger's EventName unspecified command does get called but on AnimationLoaded event only:
<i:EventTrigger>
<i:InvokeCommandAction Command="{Binding SomeCommand}"/>
</i:EventTrigger>
In this situation command is called properly. However, I'm interested in AnimationCompleted event.
P.S. Everything works fine without MVVM.
Related
From what I read in the description of Caliburn Micro, this code should be compiled without problems. Caliburn Description
<Button>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cal:ActionMessage MethodName="AbrirPDF">
<cal:Parameter Value="{Binding CNPJ}"/>
</cal:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
When trying this, I get the following error:
ArgumentException: Cannot add instance of type 'ActionMessage' to a collection of type 'TriggerActionCollection'. Only items of type 'T' are allowed.
Could someone help me solve this problem?
You probably have the wrong namespace mappings. This should compile:
<UserControl x:Class="CaliburnMicroSample.Views.ShellView"
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"
xmlns:cal="http://www.caliburnproject.org">
<StackPanel>
<Button>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cal:ActionMessage MethodName="AbrirPDF">
<cal:Parameter Value="{Binding CNPJ}"/>
</cal:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
</UserControl>
The only installed NuGet package is Caliburn.Micro 3.2.0.
I had similar issues I had to add an extra xaml tag before calling ActionMessage my corresponding sample to get it to work was:
<StackPanel>
<Button>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cal:Action.Target>
<cal:ActionMessage MethodName="AbrirPDF">
<cal:Parameter Value="{Binding CNPJ}"/>
</cal:ActionMessage>
</cal:Action.Target>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
Then I was able to access the specified tag.
For Caliburn.Micro v4 I found that changing the namespaces to the following fixed my issue and I did not have to add the extra <cal:Action.Target> tags
xmlns:cal="http://caliburnmicro.com"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
Previously I had been using the following namespaces
xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro.Platform"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
I spend a lot of time to understand why it is not working, it just MUST work but it doesn't...
Let's imagine I have ItemsControl binded to ObservableCollection. Inside ItemsControl.ItemTemplate there are StackPanel with buttons, textblocks etc.
I want to bind ToolTip Opened event attached to StackPanel to my Command, and I need instance of model also.
<StackPanel.ToolTip>
<ToolTip>
<TextBlock Text="Example text"></TextBlock>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Opened">
<i:InvokeCommandAction Command="{Binding Path=DataContext.TooltipOpenCommand, ElementName=MapsControl}" CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ToolTip>
</StackPanel.ToolTip>
My C# code is:
public ICommand TooltipOpenCommand => new RelayCommand(ToolTipOpen);
public void ToolTipOpen(object obj)
{
MessageBox.Show("Test message");
}
(RelayCommand is just default Command example from internet with execute and CanExecute)
So, when I hover mouse on StackPanel, tooltip with "Example text" is appears but command is not executed, I can't ever understand why, what am I doing wrong?
By the way, to prove that it MUST work, check this code(it attached just to the same StackPanel in example above, but not to tooltip). AND THIS CODE IS WORKING!!! But the code above isn't...
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<i:InvokeCommandAction Command="{Binding Path=DataContext.TooltipOpenCommand, ElementName=MapsControl}"
CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
*I'm using Material Design though this shouldn't be relative to the question
<ScrollViewer>
<ListView>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListView>
</ScrollViewer>
Sofar I've gotten it to trigger the command on double click, although I want to also set a command target property like:
CommandTarget="{Binding ElementName=addhost}"
Is it possible to use MVVMLight Toolkits EventToCommand approach for handling Datagrid's AutoGeneratingColumn event? I tried the following approach, but it doesn't seem to work:
<i:Interaction.Triggers>
<i:EventTrigger EventName="AutoGeneratingColumn">
<mvvm:EventToCommand Command="{Binding TestCommand}"> </mvvm:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
Yes it is.
I'd recommend setting your Xaml and Command Signature as I've outlined below.
XAML
<i:Interaction.Triggers>
<i:EventTrigger EventName="AutoGeneratingColumn">
<mvvm:EventToCommand Command="{Binding TestCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
C# - In your DataContext (ViewModel)
public void Test(AutoGeneratingColumnEventArgs e)
{
// handle however you need to here
}
I have a toggle button in my silverlight application as follows:
<ToggleButton Content="ToggleButton" Margin="0,30,0,0" Style="{StaticResource ChkToggleButton}" />
The toggle currently is changing visual states on toggle and nothing more.
However, I need to bind each of the toggle with a different command. For eg: On button press1, run command1 and on button press again, run command2.
How can this be done?
Use Triggers for such purposes:
<ToggleButton Content="ToggleButton" Margin="0,30,0,0" Style="{StaticResource ChkToggleButton}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding FirstCommand}"
CommandParameter="{Binding SomeParameter}" />
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<i:InvokeCommandAction Command="{Binding SecondCommand}"
CommandParameter="{Binding AnotherParameter}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ToggleButton>
If you don't have Interactivity you can get it by installing Expression Blend SDK or as a NuGet package.
use a single command and also keep track of the toggle state.
Have a viewmodel (preferably, or some codebehind) and let it use these two inputs to decide what actually needs to be done.