WPF modal dialog on touch screen - c#

I am using a custom modal dialog in a WPF application, that will run on a touch device (Windows 10). I am using the MVVM pattern.
My problem:
I experience different touch behavior depending on from where the dialog is opened. If it is opened from, for example, a button event handler or via button command binding, everything works fine. Then dialog buttons (typically "OK" and "Cancel") closes the dialog on first touch. But when the dialog is opened from code (not driven by button event or button command binding), the dialog buttons does not respond on the first touch. I then need to touch somewhere in the dialog before touching the button. It is kind of annoying.
I have a view model with an ExitCommand implemented that opens the dialog. Some XAML examples:
This works fine:
<Button Content="Exit" Command="{Binding ExitCommand}"/>
This does not work satisfactory:
<Label Content="Exit">
<Label.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding ExitCommand}"/>
</Label.InputBindings>
</Label>
And as mentioned, opening the dialog from code without any user action, does not work either.
Clues, anyone?

I'm not sure that this solution will solve your problem but try this please:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<Label Content="Exit">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<!-- MouseDown trigger for touch down too. -->
<i:InvokeCommandAction Command="{Binding ExitCommand}"
x:Name="exitCommandMouse" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Label>
I use a similar solution with grid (instead label) and its works perfectly with touch screen on win 8.1.

Related

Remove focus from textbox when clicking on parent grid in MVVM

I have a simple window that has a few buttons and a textbox. I want that when I press "Enter" on my keyboard this will trigger a button command but the problem is that when I'm focused on the textbox, I can't press "Enter" because it will just add a linebreak to the textbox.
I tried binding a LeftClick gesture of the grid to a command that uses keyboard.ClearFocus() but I guess that isn't the right function because that didn't work.
Another option which might work but I didn't try yet and I'm not sure if it's even a good practice, is adding x:Name to the textbox, and sending the window as the parameter to the command.
I want that when I click the parent grid of the textbox the focused will be removed from it so I can press "Enter", is there a simple way to do it in MVVM?
You said: "I want that when I press Enter on my keyboard this will trigger a button command"
Have you tried adding InputBindings to the text box?
<TextBox.InputBindings>
<KeyBinding Key="Return" Command="{Binding YourCommand}" CommandParameter="{Binding Path=Text, RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}"/>
</TextBox.InputBindings>

Add Drag&Drop with Behaviors

I need to modify a desktop application which uses WPF, MVVM and Behaviors for event handling. I've got a task to implement Drag&Drop for a button. If the user presses the button it will popup a file-save window but if the user clicks and drags it, it should display a file icon and the let the user drop it into an Explorer window to save it there.
I've already added namespaces:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:behaviors="clr-namespace:MyApplication.Desktop.Client.Behaviors"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:command="http://www.galasoft.ch/mvvmlight"
I've also added XAML code into the button:
<Button Grid.Column="2"
Command="{Binding SaveAttachmentCommand}"
Visibility="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, Converter={StaticResource boolToVisibilityConverter}}"
Style="{StaticResource AttachmentSaveButtonStyle}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown">
<command:EventToCommand Command="{Binding LeftMouseButtonDownCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<i:Interaction.Behaviors>
<behaviors:FrameworkElementDragBehavior>
</behaviors:FrameworkElementDragBehavior>
</i:Interaction.Behaviors>
</Button>
But I don't know how to tell the behavior class (FrameworkElementDragBehavior) which events to handle and how to handle them (what functions to call).
I've read some tutorials but I'm still confused.
I had to do Drag and drop with MVVM two months ago.
After some research, personnaly, the best way to achieve that is to work with the "GongSolutions DragDrop" library.
It's very simple and it's perfect for what you're looking for.
For example, in a treeview:
<TreeView ItemsSource="{Binding LstCat}"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True"
dd:DragDrop.DragAdornerTemplate="{StaticResource DragAdorner}">
//Treeview Structure
</TreeView>
From there you can do Drag&Drop in the treeview. You can add a dragAdorner too (an image next to your pointer when you are dragging something)
In the viewModel you can tell the behaviour of the dragging or Dropping by implementing an interface which comes with the library. This way you can access to the data your dragging.
For example:
public void DragOver(IDropInfo dropInfo)
{
if (dropInfo.Data is Category && dropInfo.TargetItem is Rubrique)
{
return;
}
dropInfo.DropTargetAdorner = DropTargetAdorners.Highlight;
dropInfo.Effects = DragDropEffects.Move;
}
Here is the link of the library if you're interested:
https://github.com/punker76/gong-wpf-dragdrop

Change visual state with button

I'm currently developing an app on UWP platform, even though its not of high importance since its school work only, I wish to make some smooth transitions at menu opening. I made two states of menu but I just can't figure out how to change state when "open menu" button is pressed, does anyone have any idea how to do that?
BTW menu is an StackPanel, hope I choose it rights
Thanks for your time
I believe this should do the trick:
Add these to the owning window/page/control:
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
And set the button as such:
<Button x:Name="TheButton"
Content="Transition">
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<core:GoToStateAction StateName="TheStateToMoveTo"
TargetObject="{Binding ElementName=NameOfTheObjectWithTheStates}"
UseTransitions="True" />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</Button>
The event should be caught by the trigger and the behavior should make the state transition occur.

Why does PopupWindowAction class sustain modal mode poorly for dialogs in Prism 6?

I use PopupWindowAction class for creation of modal dialog windows (customized and predefined) in my Prism 6 WPF modular application with Unity. (I have Windows 10 on my computer and use MS VS 2015 Professional as development environment.) This is for customized modal dialog:
<i:Interaction.Triggers>
<prism:InteractionRequestTrigger SourceObject="{Binding LoginConfirmationRequest, Mode=OneWay}">
<prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True">
<prism:PopupWindowAction.WindowContent>
<views:LoginView />
</prism:PopupWindowAction.WindowContent>
</prism:PopupWindowAction>
</prism:InteractionRequestTrigger>
</i:Interaction.Triggers>
Where LoginView is Prism UserControl(WPF) used for representation of login dialog content. XAML below is for predefined dialog:
<i:Interaction.Triggers>
<prism:InteractionRequestTrigger SourceObject="{Binding NotificationRequest, Mode=OneWay}">
<prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True"/>
</prism:InteractionRequestTrigger>
</i:Interaction.Triggers>
But there is a problem: When dialog (indifferently customized or predefined but with IsModal="True" property of PopupWindowAction) is displayed atop Shell and I begin (in order to verify that dialog is modal) perform frequent clicks by left mouse button on Shell area around the dialog then the dialog is going to background and Shell is displaying atop of the dialog. I just notice it not only in my application but also in InteractivityQuickstart example application. Why is this happening? How to set strong modality for customized and predefined dialogs? Please help me.
This has been fixed in the latest Pre-release, and the fix will be made available with the next RTM release of Prism 6.2
https://github.com/PrismLibrary/Prism/wiki/Release-Notes--Jan-10,-2016#prism-for-wpf-611-pre2
The issue was that the window owner was never being set.

Disabling global KeyBindings in some controls in wpf c#

I have a main window with some keybindings like this:
<Window.InputBindings>
<KeyBinding Command="{Binding RotateCommand}"
CommandParameter="left"
Key="L">
</KeyBinding>
<KeyBinding Command="{Binding RotateCommand}"
CommandParameter="right"
Key="R" />
</Window.InputBindings>
The problem is that I am unable to type this letters in any textboxes in that window, keys L and R just call commands.
Is there any way to disable KeyBindings inside of some controls? (Maybe when some controls have a focus).
I search through the internet and find this, but I can't get access to CanExecute routed event because I'm using mvvm model for command bindings.

Categories