I'm writing a Windows Phone 8.1 app and I would like to get binding Tapped event (e.g. for Grid) capability. I read many articles and answers, but all solutions don't work in my situation. Does it exist any alternative to triggers (System.Windows.Interactivity) or Command for WP 8.1?
Thanks for interest, but I found solution. I probably was too tired and I forgot add reference to Behaviors SDK. It works great now.
<Page
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core" ...>
And simple Command usage:
<SomeControl ...>
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<core:InvokeCommandAction Command="{Binding MyCommand}" />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</SomeControl>
Related
I'm working on a WPF project using MVVM Light and trying to use an EventToCommand, but I get an error message saying:
A value of type 'EventToCommand' cannot be added to a collection or dictionary of type 'TriggerActionCollection'."
I'm using .NET Framework 4.7.2 and the NuGet library Microsoft.Xaml.Behaviors.Wpf.
I've seen most places talking about http://schemas.microsoft.com/expression/2010/interactivity,
but from what I've understood this is now deprecated, and http://schemas.microsoft.com/xaml/behaviors should be used instead.
On mvvmlight.net's changelog I found this:
EventToCommand -
BL0004.005, The type 'EventToCommand' from assembly 'GalaSoft.MvvmLight.Extras.WPF4' is built with an older version of the Blend SDK, and is not supported in a Windows Presentation Framework 4 project.
I've tried changing the projects target framework version, but it didn't seem to make a difference.
Any help would be appreciated.
<UserControl x:Class="DialogueTree.View.DialogueControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:local="clr-namespace:DialogueTree.View"
xmlns:command="http://www.galasoft.ch/mvvmlight"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="200"
MinHeight="200" MinWidth="200">
<TextBox Text="{Binding Headline}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="LostFocus">
<command:EventToCommand Command="{Binding Source={StaticResource Locator}, Path=DialogueControlVM.EndEditHeadline}"
CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
The EventToCommand action from MvvmLight is based on the legacy Blend SDK assemblies.
XML namespace http://schemas.microsoft.com/expression/2010/interactivity
CLR namespace System.Windows.Interactivity
These are in fact deprecated and replaced by the open source XAML behaviors that you use. The issue is that these assemblies are incompatible, so you cannot use the actions provided by MvvmLight with the XAML behaviors and vice-versa. I guess MvvmLight will be updated to use these behaviors in the future, too. Here is an open issue on this topic.
However, you do not need EventToCommand, as the XAML behaviors package already contains a command that does the same: InvokeCommandAction. The property names might be different, though.
<b:EventTrigger EventName="LostFocus">
<b:InvokeCommandAction Command="{Binding Source={StaticResource Locator}, Path=DialogueControlVM.EndEditHeadline}"
CommandParameter="{Binding}"/>
</b:EventTrigger>
I am using Caliburn Micro for a Windows Phone app. I have a hyperlink control for which I want to bind the click event to my View Model. Below is the sample code
XAML, MyPage.xaml
<TextBlock>
<Run>Got to</Run>
<Hyperlink micro:Message.Attach="[Event Click] = [Action OpenAnotherPage]">
My Page</Hyperlink><Run Text="."></Run></TextBlock>
ViewModel MyPageViewModel.cs
public void OpenAnotherPage()
{
// some code
}
When I tap on the link, I get an exception
System.Exception: No target found for method
What could be the problem?
Update 1: Tried setting micro:Action.TargetWithoutContext="{Binding ElementName=MyPage, Path=DataContext}" on the Hyperlink control, but it didn't work
Give the TextBlock a name and use that as the ElementName since you have the Hyperlink nested into that control. Then Update1 should work.
Try following XAML (untested):
<Hyperlink>My Page
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ec:CallMethodAction TargetObject="{Binding}" MethodName="OpenAnotherPage" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Hyperlink>
The namespaces are following (you'll need to reference those 2 assemblies, they're by Microsoft and shipped with MS Expression Blend)
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ec="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
I'm trying to add a Blend behavior to a DatePicker control to bind an MVVM-Light RelayCommand to the DateChanged event like so:
<DatePicker Date="{Binding SelectedDate, Mode=TwoWay}">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="DateChanged">
<Core:InvokeCommandAction Command="{Binding DateChangedCommand, Mode=OneWay}"/>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</DatePicker>
I'm using the following definitions:
xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
However, I keep getting the following error:
WinRT information: Cannot add instance of type
'Microsoft.Xaml.Interactions.Core.EventTriggerBehavior' to a collection of type
'Microsoft.Xaml.Interactivity.BehaviorCollection'.
I have successfully used Blend behaviors in this way many other times in my project (and with other controls in the same Xaml file) to bind an event to a command, and the DatePicker control is the only one that has thrown an error. Is there another way to accomplish this or are WinRT DatePickers limited in this way?
Does exist a similar EventToCommand (behavior's) on Android?
Example Silverlight/WPF:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<Commanding:EventToCommand Command="{Binding DataContext.EditEventTypeCommand,
RelativeSource={RelativeSource AncestorType=telerik:RadGridView}}"
CommandParameter="{Binding}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
If you are using mvvmcross, then bindings exist for all events which have EventHandler signatures - so you can just use bindings like:
'Click':{'Path':'MyViewModelCommand'}
There are plenty of examples of this in the samples, plus there are several questions oh here - search for MvvmCross and click.
If you need to bind to an event which has an EventHandler<T> signature then you can add this yourself - see mvvmcross touch command binding in android
I'm not sure I have understood your question (I don't know Silverlight) but you can do as follows:
In your XML:
<Button ...
android:onClick="onMyButtonClicked".../>
In your java file
public void onMyButtonClicked(View sender) { ... }
There are no other ways to specify event handlers in Android XML, I think. You can specify a lot of properties though.
Does anybody know whether one can trigger prism command with a shortcut? What I mean is I want to be able to define binding of a command to keyboard shortcut in declarative manner, like ClientUI does:
Are there any opensource libraries for that purpose? Or maybe code samples?
I found this question but I don't think that it answers mine.
I've created such gesture trigger. And I'd like to share it with you guys. Basically, it is System.Windows.Interactivity trigger which can parse gestures represented as strings. Usage is as simple as in ClientUI:
<UserControl>
<i:Interaction.Triggers>
<behaviors:KeystrokeCommandTrigger Command="{Binding SaveChangesCommand}" Gesture="Ctrl+Shift+S" />
<behaviors:KeystrokeCommandTrigger Command="{Binding RejectChangesCommand}" Gesture="Ctrl+Shift+R" />
<behaviors:KeystrokeCommandTrigger Command="{Binding NewItemCommand}" Gesture="Ins" />
<behaviors:KeystrokeCommandTrigger Command="{Binding DeleteSelectedItemCommand}" Gesture="Del" />
<behaviors:KeystrokeCommandTrigger Command="{Binding UploadSomethingCommand}" Gesture="Ctrl+Shift+U" />
</i:Interaction.Triggers>
</UserControl>
The code is on pastie.
You could write an attached behavior that has a listens to the KeyUp event and then calls the Command. The complication comes in translating something like Gesture="Ctrl+Shift+A". You would need to write a parser to figure out exactly what key combination that string represents.