Without Interactivity.dll: Alternative to MVVMLight's EventToCommand - c#

In our current C# project, we use the MVVM light framework and its EventToCommand feature to bind events to commands (as also mentioned in this SO thread).
We use it for example like this:
<i:Interaction.Triggers>
<i:EventTrigger EventName="GotFocus">
<cmd:EventToCommand CommandParameter="{Binding SelectedItem, ElementName=SelectTree}"
Command="{Binding Path=SelectTreeGotFocusCommand, Mode=OneWay}" />
</i:EventTrigger>
</i:Interaction.Triggers>
My question is: What is the easiest way to achieve the same behaviour (i.e., EventToCommand, not limited to GotFocus) without using System.Windows.Interactivity?
To avoid the XY problem, here is some background:
We are developing an addon for an already existing main application, on which we do not have any influence. Our addon is served as a dll which is used by this main application.
So far, we used XAML code as given above (i.e., using System.Windows.Interactivity and EventToCommand). However, the last release of the main application contains an old version of System.Windows.Interactivity (so far, it was not included at all) which doesn't support this.
This means that using the old Interactivity.dll will cause our addon to be non-functional. However, overwriting the dll with the new version we need will cause issues with parts of the main application.
I read about using two different versions of the same dll in a project (for example here) but this seems to be quite involved. Since there are only a few places we use this construction, I thought that replacing it with code not using Interactivity.dll might be easier.

If you switch to the Microsoft.Xaml.Behaviors.Wpf nuget package and change your namespace declarations to
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
you can use the InvokeCommandAction like so:
<i:Interaction.Triggers>
<i:EventTrigger EventName="GotFocus"}">
<i:InvokeCommandAction Command="..." CommandParameter="..."/>
</i:EventTrigger>
</i:Interaction.Triggers>

Related

HelixViewport3d Ctrl + C

I am using HelixToolkit.Wpf in a WPF / .Net Framework 4.7.2 application. I am trying to make a copy-paste functionality. To achive it, I have the following code in my xaml file:
<KeyBinding Key="C" Modifiers="Control" Command="{Binding CopyCommand}" />
<KeyBinding Key="V" Modifiers="Control" Command="{Binding PasteCommand}" />
The PasteCommand is called as expected, but the CopyCommand is not.
When profiling the app, I can see that large MemoryStreams are created when I type Ctrl+C on the keyboard and I can see that the CPU is spending a lot of time in HelixToolkit.Wpf.HelixViewport3D.Copy(). I think it is copying the screen view as a bitmap or something like that.
It looks to me like the "Ctrl+C" is already pre-assigned behind the scenes, but I cannot find any documentation about it.
How would you overwrite this key binding to call my own copy code?

WPF Button's Command-Binding not working when launched from WinForms

I have a UserControl with two buttons in my Window. The UserControl has it's own DataContext with two corresponding Commands.
Naturally, the binding on the buttons was written like this
Command="{Binding SaveCommand}"
However, we have two launch mechanisms for our modules: one is via a WPF launcher and the other is via our old WinForms menu. If we use the WinForms menu, the binding does not work anymore. Instead the following Binding was the only way to make it work.
Command="{Binding DataContext.SaveCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
Is this due to WinForms in combination with the way commands bubble up or something along those lines?
Note 1: Other modules seem not to be afflicted somehow. It might really be this exact structure / the double DataContext (window/control)
Note 2: It might be worth noting that we use Telerik's RadButton instead of regular buttons.

How should I binding xaml event from WPF in .net core 3.0?

How should I binding a event in netcore 3.0?
In a WPF project(netcore3.0), there is no kind of Interactive.dll to do like
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding Path=DoSomethingCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
so how to binding a event in WPF (netcore3.0)?
You should be able to use a mousebinding for this.
<YourControl.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick" Command="... />
</YourControl.InputBindings>
The way you're intended to use that dll now is via a nuget package - xaml bahaviors.
https://devblogs.microsoft.com/dotnet/open-sourcing-xaml-behaviors-for-wpf/
Seeing as how net core 3 is still only in preview, i thought it might be rather early for this package to be updated.
Seems not though:
https://github.com/microsoft/XamlBehaviorsWpf/issues/13

Get location from bing map on Tap event in mvvm

i'm trying to get location, when I tap on bing map control. I know how to get it, but I want to make it in ViewModel. For Tap event I use interactions.
<map:Map CopyrightVisibility="Collapsed" LogoVisibility="Collapsed">
<map:MapTileLayer>
<map:MapTileLayer.TileSources>
<customMap:OpenStreetTile />
</map:MapTileLayer.TileSources>
</map:MapTileLayer>
<map:MapLayer>
<map:Pushpin Location="{Binding Location}" />
</map:MapLayer>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<cmd:EventToCommand Command="{Binding AddPushpinCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</map:Map>
I can get position from eventarguments with GetPosition function. But this function need UIElement parameter.I made it so:
var source = (e.OriginalSource as MapLayer).ParentMap;
var point = e.GetPosition(source);
Location = source.ViewportPointToLocation(point);
But Im not sure if it is good solution. Do you have any idea how to do it?
Thanks
Well, your approach certainly works and sometimes, good enough is good enough. ;) If you want to be 100% clean with your MVVM implementation, you should avoid references to the view (and thus the MapLayer class) however.
Joost van Schaik wrote a blog post that shows how to achieve this using behaviors (it's for Windows Phone 8, but I'm sure it applies to Windows Phone 7 as well): http://dotnetbyexample.blogspot.nl/2013/03/simple-reverse-geocoding-with-windows.html

Attach Prism Command to Grid?

Is it possible to create a command behavior using Prism's CommandBehaviorBase class for Silverlight's grid? I know that it is only intended for actual controls, so I was wondering if anyone might know if a workaround. I would like to create an attachable mouse over behavior for a grid, that executes a specific command, and ideally would like to use Prism for this approach, just can't seem to use CommandBehaviorBase for a Grid.
Thanks.
The arguably easier way to achieve this is to use Triggers. Doesn't require you to write any code, all you have to do is this:
<Grid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<si:InvokeDataCommand Command="{Binding DoSomethingCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
...
</Grid>
Here the DoSomethingCommand (defined in a ViewModel) will trigger when MouseEnter event is fired on the Grid.

Categories