In WPF I've got the following XAML:
<ScrollViewer Canvas.Left="2266" Canvas.Top="428" Height="378" Name="scrollViewer1" Width="728" PanningMode="VerticalOnly" PanningRatio="2">
<Canvas Height="1732.593" Width="507.667">
<Slider Height="40.668" x:Name="slider1" Width="507.667" Style="{DynamicResource SliderStyle1}" Canvas.Left="15" Canvas.Top="150" />
</Slider>
</Canvas>
</ScrollViewer>
It's a ScrollViewer containing a Slider. I'm using the following on a touch-screen, and I'm using the panning even to scroll the ScrollViewer vertically. When PanningMode="VerticalOnly" is set, the slider stops working!
I'm assuming the ScollViewer is consuming the touch\slide event and handling it before the slider does (but I think I'm wrong on this front).
Is there any workaround for this?
I just solved this issue in our app.
What is happening is that the ScrollViewer captures the TouchDevice in its PreviewTouchMove handler, which "steals" the TouchDevice from other controls and prevents them from receiving any PreviewTouchMove or TouchMove events.
In order to work around this, you need to implement a custom Thumb control that captures the TouchDevice in the PreviewTouchDown event and stores a reference to it until the PreviewTouchUp event occurs. Then the control can "steal" the capture back in its LostTouchCapture handler, when appropriate. Here is some brief code:
public class CustomThumb : Thumb
{
private TouchDevice currentDevice = null;
protected override void OnPreviewTouchDown(TouchEventArgs e)
{
// Release any previous capture
ReleaseCurrentDevice();
// Capture the new touch
CaptureCurrentDevice(e);
}
protected override void OnPreviewTouchUp(TouchEventArgs e)
{
ReleaseCurrentDevice();
}
protected override void OnLostTouchCapture(TouchEventArgs e)
{
// Only re-capture if the reference is not null
// This way we avoid re-capturing after calling ReleaseCurrentDevice()
if (currentDevice != null)
{
CaptureCurrentDevice(e);
}
}
private void ReleaseCurrentDevice()
{
if (currentDevice != null)
{
// Set the reference to null so that we don't re-capture in the OnLostTouchCapture() method
var temp = currentDevice;
currentDevice = null;
ReleaseTouchCapture(temp);
}
}
private void CaptureCurrentDevice(TouchEventArgs e)
{
bool gotTouch = CaptureTouch(e.TouchDevice);
if (gotTouch)
{
currentDevice = e.TouchDevice;
}
}
}
Then you will need to re-template the Slider to use the CustomThumb instead of the default Thumb control.
i strugled with a similar issue. the workaround was this one (none of the others worked for me): i created a custom thumb, and then i used it inside a scrollbar style in xaml as the PART_Track's thumb.
public class DragableThumb : Thumb
{
double m_originalOffset;
double m_originalDistance;
int m_touchID;
/// <summary>
/// Get the parent scrollviewer, if any
/// </summary>
/// <returns>Scroll viewer or null</returns>
ScrollViewer GetScrollViewer()
{
if (TemplatedParent is ScrollBar && ((ScrollBar)TemplatedParent).TemplatedParent is ScrollViewer)
{
return ((ScrollViewer)((ScrollBar)TemplatedParent).TemplatedParent);
}
return null;
}
/// <summary>
/// Begin thumb drag
/// </summary>
/// <param name="e">Event arguments</param>
protected override void OnTouchDown(TouchEventArgs e)
{
ScrollViewer scrollViewer;
base.OnTouchDown(e);
m_touchID = e.TouchDevice.Id;
if ((scrollViewer = GetScrollViewer()) != null)
{
m_originalOffset = scrollViewer.HorizontalOffset;
m_originalDistance = e.GetTouchPoint(scrollViewer).Position.X;
}
}
/// <summary>
/// Handle thumb delta
/// </summary>
/// <param name="e">Event arguments</param>
protected override void OnTouchMove(TouchEventArgs e)
{
ScrollViewer scrollViewer;
double actualDistance;
base.OnTouchMove(e);
if ((scrollViewer = GetScrollViewer()) != null && m_touchID == e.TouchDevice.Id)
{
actualDistance = e.GetTouchPoint(scrollViewer).Position.X;
scrollViewer.ScrollToHorizontalOffset(m_originalOffset + (actualDistance - m_originalDistance) * scrollViewer.ExtentWidth / scrollViewer.ActualWidth);
}
}
}
The following worked for me. I searched around for a long time for something that would work. I adapted this for touch from How to make WPF Slider Thumb follow cursor from any point. This is a much simpler fix and allows you to avoid creating a custom slider/thumb control.
<Slider TouchMove="OnTouchMove" IsMoveToPointEnabled="True"/>
IsMoveToPointEnable must be set to true for this to work.
private void Slider_OnTouchMove(object sender, TouchEventArgs e)
{
Slider slider = (Slider)sender;
TouchPoint point = e.GetTouchPoint (slider );
double d = 1.0 / slider.ActualWidth * point.Position.X;
int p = int(slider.Maximum * d);
slider.Value = p;
}
This is nice and simple and worked for me - although it's worth wrapping up in a generic function and extending to handle the slider minimum value also as it may not be zero. What a pain to have to do though. There are many thing about WPF that are cool, but so many simple things require extra steps it really can be detrimental to productivity.
Related
I have a UserControl that must respond to TouchUp events and this sits within a Viewbox which needs to be panned and scaled with pinch manipulation. Touch events on the control are handled fine. However pinch manipulations only scale the ViewPort if both pinch points are contained entirely within either the user control or the Viewport space around it. If the pinch straddles the user control boundary then the ManipulationDelta loses one of the points and reports a scale of (1,1).
If I remove IsManipulationEnabled="True" from the control handling the TouchUp event then the scaling works but the touch event doesn’t fire.
What can I do to retain the manipulation across the ViewPort whilst also handling the touch event in the user control?
Test Solution
<Window x:Class="TouchTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Touch Test"
Height="400"
Width="700"
ManipulationDelta="OnManipulationDelta"
ManipulationStarting="OnManipulationStarting">
<Grid Background="Transparent"
IsManipulationEnabled="True">
<Viewbox x:Name="Viewbox"
Stretch="Uniform">
<Viewbox.RenderTransform>
<MatrixTransform/>
</Viewbox.RenderTransform>
<Grid Width="800"
Height="800"
Background="LightGreen"
IsManipulationEnabled="True"
TouchUp="OnTouchUp">
<TextBlock x:Name="TimeTextBlock"
FontSize="100"
TextAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</Viewbox>
<TextBlock x:Name="ScaleTextBlock"
FontSize="10"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"/>
</Grid>
</Window>
Handlers in code-behind:
private void OnTouchUp(object sender, TouchEventArgs e)
{
TimeTextBlock.Text = DateTime.Now.ToString("H:mm:ss.fff");
}
private void OnManipulationStarting(object sender, ManipulationStartingEventArgs e)
{
e.ManipulationContainer = this;
}
private void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
if (Viewbox == null)
{
return;
}
ManipulationDelta delta = e.DeltaManipulation;
ScaleTextBlock.Text = $"Delta Scale: {delta.Scale}";
MatrixTransform transform = Viewbox.RenderTransform as MatrixTransform;
if (transform == null)
{
return;
}
Matrix matrix = transform.Matrix;
Point position = ((FrameworkElement)e.ManipulationContainer).TranslatePoint(e.ManipulationOrigin, Viewbox);
position = matrix.Transform(position);
matrix = MatrixTransformations.ScaleAtPoint(matrix, delta.Scale.X, delta.Scale.Y, position);
matrix = MatrixTransformations.PreventNegativeScaling(matrix);
matrix = MatrixTransformations.Translate(matrix, delta.Translation);
matrix = MatrixTransformations.ConstrainOffset(Viewbox.RenderSize, matrix);
transform.Matrix = matrix;
}
Supporting class:
public static class MatrixTransformations
{
/// <summary>
/// Prevent the transformation from being offset beyond the given size rectangle.
/// </summary>
/// <param name="size"></param>
/// <param name="matrix"></param>
/// <returns></returns>
public static Matrix ConstrainOffset(Size size, Matrix matrix)
{
double distanceBetweenViewRightEdgeAndActualWindowRight = size.Width * matrix.M11 - size.Width + matrix.OffsetX;
double distanceBetweenViewBottomEdgeAndActualWindowBottom = size.Height * matrix.M22 - size.Height + matrix.OffsetY;
if (distanceBetweenViewRightEdgeAndActualWindowRight < 0)
{
// Moved in the x-axis too far left. Snap back to limit
matrix.OffsetX -= distanceBetweenViewRightEdgeAndActualWindowRight;
}
if (distanceBetweenViewBottomEdgeAndActualWindowBottom < 0)
{
// Moved in the x-axis too far left. Snap back to limit
matrix.OffsetY -= distanceBetweenViewBottomEdgeAndActualWindowBottom;
}
// Prevent positive offset
matrix.OffsetX = Math.Min(0.0, matrix.OffsetX);
matrix.OffsetY = Math.Min(0.0, matrix.OffsetY);
return matrix;
}
/// <summary>
/// Prevent the transformation from performing a negative scale.
/// </summary>
/// <param name="matrix"></param>
/// <returns></returns>
public static Matrix PreventNegativeScaling(Matrix matrix)
{
matrix.M11 = Math.Max(1.0, matrix.M11);
matrix.M22 = Math.Max(1.0, matrix.M22);
return matrix;
}
/// <summary>
/// Translate the matrix by the given vector to providing panning.
/// </summary>
/// <param name="matrix"></param>
/// <param name="vector"></param>
/// <returns></returns>
public static Matrix Translate(Matrix matrix, Vector vector)
{
matrix.Translate(vector.X, vector.Y);
return matrix;
}
/// <summary>
/// Scale the matrix by the given X/Y factors centered at the given point.
/// </summary>
/// <param name="matrix"></param>
/// <param name="scaleX"></param>
/// <param name="scaleY"></param>
/// <param name="point"></param>
/// <returns></returns>
public static Matrix ScaleAtPoint(Matrix matrix, double scaleX, double scaleY, Point point)
{
matrix.ScaleAt(scaleX, scaleY, point.X, point.Y);
return matrix;
}
}
So, I'm not a wpf programmer. But have a suggestion/workaround which could possibly work for you.
You could code the thing as follows:
set IsManipulationEnabled="True" (in this case OnTouchUp isn't fired for the grid colored in LightGreen)
Set OnTouchUp to fire on either Viewbox x:Name="Viewbox" or the Grid above this Viewbox (rather than for the 800x800 Grid)
So now OnTouchUp would be fired whenever you touch anywhere in the Viewbox (not just inside the LightGreen area)
When OnTouchUp is now fired, just check if the co-ordinates are in the region of LightGreen box. If YES-> update the time, if no, leave the time as it is.
I understand this is a workaround. Still posted an answer, in case it could prove useful.
i am not sure the sample you post reflect totally your code...but what i see : you do not manage the ManipulationCompleted and LostMouseCapture. Also you do not make any MouseCapture() MouseRelease() so when the manipulation outbound the window you loose it....search "mouse capture" on this repo, you will see even if no manipulation event that this is quite complicated....https://github.com/TheCamel/ArchX/search?utf8=%E2%9C%93&q=mouse+capture&type=
I'm trying to improve the responsiveness of a WPF business application so that when users are "between" screens waiting for a new screen to appear after a server response, they can still be entering data. I'm able to queue the events (using a PreviewKeyDown event handler on background panel) but then I'm having difficulties just throwing the events I dequeue back at the new panel once it's loaded. In particular TextBoxes on the new panel are not picking up the text. I've tried raising the same events (setting Handled to true when capturing them, setting Handled to false when raising them again) creating new KeyDown events, new PreviewKeyDown events, doing ProcessInput, doing RaiseEvent on the panel, setting the focus on the right TextBox and doing RaiseEvent on the TextBox, many things.
It seems like it should be really simple, but I can't figure it out.
Here are some of the things I've tried. Consider a Queue of KeyEventArgs called EventQ:
Here's one thing that doesn't work:
while (EventQ.Count > 0)
{
KeyEventArgs kea = EventQ.Dequeue();
tbOne.Focus(); // tbOne is a text box
kea.Handled = false;
this.RaiseEvent(kea);
}
Here's another:
while (EventQ.Count > 0)
{
KeyEventArgs kea = EventQ.Dequeue();
tbOne.Focus(); // tbOne is a text box
var key = kea.Key; // Key to send
var routedEvent = Keyboard.PreviewKeyDownEvent; // Event to send
KeyEventArgs keanew = new KeyEventArgs(
Keyboard.PrimaryDevice,
PresentationSource.FromVisual(this),
0,
key) { RoutedEvent = routedEvent, Handled = false };
InputManager.Current.ProcessInput(keanew);
}
And another:
while (EventQ.Count > 0)
{
KeyEventArgs kea = EventQ.Dequeue();
tbOne.Focus(); // tbOne is a text box
var key = kea.Key; // Key to send
var routedEvent = Keyboard.PreviewKeyDownEvent; // Event to send
this.RaiseEvent(
new KeyEventArgs(
Keyboard.PrimaryDevice,
PresentationSource.FromVisual(this),
0,
key) { RoutedEvent = routedEvent, Handled = false }
);
}
One strange thing I've noticed is that when using the InputManager method (#2) spaces do appear. But normal text keys do not.
The same resources turned up for me when I did some research, so I think what you do in your answer is pretty valid.
I looked on and have found another way of doing it, using the Win32 API. I had to introduce some threading and small delays, because for some reason the key events were not replayed in the correct sequence without that. Overall I think this solution is easier though, and I also figured out how to include modifier keys (by using the Get/SetKeyboardState function). Uppercase is working, and so should keyboard shortcuts.
Starting the demo app, pressing the keys 1 space 2 space 3 tab 4 space 5 space 6, then clicking the button produces the following:
Xaml:
<UserControl x:Class="WpfApplication1.KeyEventQueueDemo"
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"
mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" >
<StackPanel>
<TextBox x:Name="tbOne" Margin="5,2" />
<TextBox x:Name="tbTwo" Margin="5,2" />
<Button x:Name="btn" Content="Replay key events" Margin="5,2" />
</StackPanel>
</UserControl>
Code behind:
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
namespace WpfApplication1
{
/// <summary>
/// Structure that defines key input with modifier keys
/// </summary>
public struct KeyAndState
{
public int Key;
public byte[] KeyboardState;
public KeyAndState(int key, byte[] state)
{
Key = key;
KeyboardState = state;
}
}
/// <summary>
/// Demo to illustrate storing keyboard input and playing it back at a later stage
/// </summary>
public partial class KeyEventQueueDemo : UserControl
{
private const int WM_KEYDOWN = 0x0100;
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
[DllImport("user32.dll")]
static extern bool GetKeyboardState(byte[] lpKeyState);
[DllImport("user32.dll")]
static extern bool SetKeyboardState(byte[] lpKeyState);
private IntPtr _handle;
private bool _isMonitoring = true;
private Queue<KeyAndState> _eventQ = new Queue<KeyAndState>();
public KeyEventQueueDemo()
{
InitializeComponent();
this.Focusable = true;
this.Loaded += KeyEventQueueDemo_Loaded;
this.PreviewKeyDown += KeyEventQueueDemo_PreviewKeyDown;
this.btn.Click += (s, e) => ReplayKeyEvents();
}
void KeyEventQueueDemo_Loaded(object sender, RoutedEventArgs e)
{
this.Focus(); // necessary to detect previewkeydown event
SetFocusable(false); // for demo purpose only, so controls do not get focus at tab key
// getting window handle
HwndSource source = (HwndSource)HwndSource.FromVisual(this);
_handle = source.Handle;
}
/// <summary>
/// Get key and keyboard state (modifier keys), store them in a queue
/// </summary>
void KeyEventQueueDemo_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (_isMonitoring)
{
int key = KeyInterop.VirtualKeyFromKey(e.Key);
byte[] state = new byte[256];
GetKeyboardState(state);
_eventQ.Enqueue(new KeyAndState(key, state));
}
}
/// <summary>
/// Replay key events from queue
/// </summary>
private void ReplayKeyEvents()
{
_isMonitoring = false; // no longer add to queue
SetFocusable(true); // allow controls to take focus now (demo purpose only)
MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); // set focus to first control
// thread the dequeueing, because the sequence of inputs is not preserved
// unless a small delay between them is introduced. Normally the effect this
// produces should be very acceptable for an UI.
Task.Run(() =>
{
while (_eventQ.Count > 0)
{
KeyAndState keyAndState = _eventQ.Dequeue();
Application.Current.Dispatcher.BeginInvoke((Action)(() =>
{
SetKeyboardState(keyAndState.KeyboardState); // set stored keyboard state
PostMessage(_handle, WM_KEYDOWN, keyAndState.Key, 0);
}));
System.Threading.Thread.Sleep(5); // might need adjustment
}
});
}
/// <summary>
/// Prevent controls from getting focus and taking the input until requested
/// </summary>
private void SetFocusable(bool isFocusable)
{
tbOne.Focusable = isFocusable;
tbTwo.Focusable = isFocusable;
btn.Focusable = isFocusable;
}
}
}
The enqueue system is something that I've wanted to do myself, as part of my project which allows multi-threaded UI to function without any problems(one thread routes events into another). There is only slight problem, namely WPF does not have public API to inject INPUT events. Here is a copy/paste from one of the Microsoft employees that I talked with, like weeks back:
"WPF does not expose public methods for injecting input events in the proper way. This scenario is just not supported by the public API. You will probably have to do a lot of reflection and other hacking. For example, WPF treats some input as “trusted” because it knows it came from the message pump. If you just raise an input event, the event will not be trusted."
I think you need to rethink your strategy.
Thanks all for your support but I haven't really struck a solution from the SO community so I'm going to answer this myself since this is the closest I seem to get to a solution. The "hack" as Erti-Chris says seems to be what we're left with. I've had some luck decomposing the problem so I don't have the sense I'm writing a whole new keyboard handler. The approach I'm following is to decompose the events into a combination of InputManager handling and of TextComposition. Throwing a KeyEventArgs (either the original one or one I've created myself) doesn't seem to register on a PreviewKeyDown handler.
Part of the difficulty comes from the information in Erti-Chris's post, and another part seems to be related to TextBoxes trying to react to certain keys like arrow keys differently from normal keys like the letter "A".
To move forward with this I found information from this post to be useful:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b657618e-7fc6-4e6b-9b62-1ffca25d186b
Here is the solution that I'm getting some positive results from now:
Keyboard.Focus(tbOne); // the first element on the Panel to get the focus
while (EventQ.Count > 0)
{
KeyEventArgs kea = EventQ.Dequeue();
kea.Handled = false;
var routedEvent = KeyDownEvent;
KeyEventArgs keanew = new KeyEventArgs(
Keyboard.PrimaryDevice,
PresentationSource.FromVisual(tbOne),
kea.Timestamp,
kea.Key) { RoutedEvent = routedEvent, Handled = false };
keanew.Source = tbOne;
bool itWorked = InputManager.Current.ProcessInput(keanew);
if (itWorked)
{
continue;
// at this point spaces, backspaces, tabs, arrow keys, deletes are handled
}
else
{
String keyChar = kea.Key.ToString();
if (keyChar.Length > 1)
{
// handle special keys; letters are one length
if (keyChar == "OemPeriod") keyChar = ".";
if (keyChar == "OemComma") keyChar = ",";
}
TextCompositionManager.StartComposition(new TextComposition(InputManager.Current, Keyboard.FocusedElement, keyChar));
}
}
If anyone can show me a better way I'm delighted to mark your contribution as the answer, but for now this is what I'm working with.
Scenario:
User clicks a button on the View
This invokes a command on the ViewModel, DoProcessing
How, and where does the Wait cursor get set, considering the responsibilitues of View and ViewModel?
Just to be clear, I am just looking to change the DEFAULT cursor to an hourglass while the command is running. When the command completes, the cursor mut change back to an arrow. (It is a synchronous operation I am looking for, and I want the UI to block).
I have created an IsBusy property on the ViewModel. How do I ensure that the Application's mouse pointer changes?
I am using it successfully in my application:
/// <summary>
/// Contains helper methods for UI, so far just one for showing a waitcursor
/// </summary>
public static class UIServices
{
/// <summary>
/// A value indicating whether the UI is currently busy
/// </summary>
private static bool IsBusy;
/// <summary>
/// Sets the busystate as busy.
/// </summary>
public static void SetBusyState()
{
SetBusyState(true);
}
/// <summary>
/// Sets the busystate to busy or not busy.
/// </summary>
/// <param name="busy">if set to <c>true</c> the application is now busy.</param>
private static void SetBusyState(bool busy)
{
if (busy != IsBusy)
{
IsBusy = busy;
Mouse.OverrideCursor = busy ? Cursors.Wait : null;
if (IsBusy)
{
new DispatcherTimer(TimeSpan.FromSeconds(0), DispatcherPriority.ApplicationIdle, dispatcherTimer_Tick, System.Windows.Application.Current.Dispatcher);
}
}
}
/// <summary>
/// Handles the Tick event of the dispatcherTimer control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private static void dispatcherTimer_Tick(object sender, EventArgs e)
{
var dispatcherTimer = sender as DispatcherTimer;
if (dispatcherTimer != null)
{
SetBusyState(false);
dispatcherTimer.Stop();
}
}
}
This has been taken from here. Courtsey huttelihut.
You need to call the SetBusyState method every time you think you are going to perform any time consuming operation. e.g.
...
UIServices.SetBusyState();
DoProcessing();
...
This will automatically change your cursor to wait cursor when the application is busy and back to normal when idle.
A very simple method is to simply bind to the 'Cursor' property of the window (or any other control). For example:
XAML:
<Window
x:Class="Example.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Cursor="{Binding Cursor}" />
ViewModel Cursor Property (Using Apex.MVVM):
private NotifyingProperty cursor = new NotifyingProperty("Cursor", typeof(System.Windows.Input.Cursor), System.Windows.Input.Cursors.Arrow);
public System.Windows.Input.Cursor Cursor
{
get { return (System.Windows.Input.Cursor)GetValue(cursor); }
set { SetValue(cursor, value); }
}
Then simply change the cursor in your view when needed...
public void DoSomethingLongCommand()
{
Cursor = System.Windows.Input.Cursors.Wait;
... some long process ...
Cursor = System.Windows.Input.Cursors.Arrow;
}
You want to have a bool property in viewmodel.
private bool _IsBusy;
public bool IsBusy
{
get { return _IsBusy; }
set
{
_IsBusy = value;
NotifyPropertyChanged("IsBusy");
}
}
Now you want to set the window style to bind to it.
<Window.Style>
<Style TargetType="Window">
<Setter Property="ForceCursor" Value="True"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsBusy}" Value="True">
<Setter Property="Cursor" Value="Wait"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Style>
Now whenever a command is being executed and your view model is is busy, it would just set the IsBusy flag and reset it when done. The Window will automatically display the wait cursor and restore the original cursor when done.
You can write the command handler function in view model something like this:
private void MyCommandExectute(object obj) // this responds to Button execute
{
try
{
IsBusy = true;
CallTheFunctionThatTakesLongTime_Here();
}
finally
{
IsBusy = false;
}
}
Command is handled on the view model, so the reasonable decission would be to do folowing:
1) Create a busy indicator service and inject it into the view model (this will allow you to replace the cursor logic with some nasty animation easily)
2) In the command handler call the busy indicator service to notify the user
I might be wrong, but it looks like you are trying to do some heavy calculations or I/O on UI thread. I highly recommend you to perform work on thread pool in this case. You can use Task and TaskFactory to easily wrap work with ThreadPool
There is a great Session(at 50:58) by Laurent Bugnion online (Creator of MVVM Light).
There's also an deepDive session available (alternatively here(at 24:47)).
In at least one of them he live codes a busy Indicator using a is BusyProperty.
The ViewModel should only decide whether it is busy, and the decision about what cursor to use, or whether to use some other technique such as a progress bar should be left up to the View.
And on the other hand, handling it with code-behind in the View is not so desirable either, because the ideal is that Views should not have code-behind.
Therefore I chose to make a class that can be used in the View XAML to specify that the cursor should be change to Wait when the ViewModel is busy. Using UWP + Prism the class definition is:
public class CursorBusy : FrameworkElement
{
private static CoreCursor _arrow = new CoreCursor(CoreCursorType.Arrow, 0);
private static CoreCursor _wait = new CoreCursor(CoreCursorType.Wait, 0);
public static readonly DependencyProperty IsWaitCursorProperty =
DependencyProperty.Register(
"IsWaitCursor",
typeof(bool),
typeof(CursorBusy),
new PropertyMetadata(false, OnIsWaitCursorChanged)
);
public bool IsWaitCursor
{
get { return (bool)GetValue(IsWaitCursorProperty); }
set { SetValue(IsWaitCursorProperty, value); }
}
private static void OnIsWaitCursorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CursorBusy cb = (CursorBusy)d;
Window.Current.CoreWindow.PointerCursor = (bool)e.NewValue ? _wait : _arrow;
}
}
And the way to use it is:
<mvvm:SessionStateAwarePage
x:Class="Orsa.Views.ImportPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mvvm="using:Prism.Windows.Mvvm"
xmlns:local="using:Orsa"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mvvm:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid.RowDefinitions>
.
.
</Grid.RowDefinitions>
<local:CursorBusy IsWaitCursor="{Binding IsBusy}"/>
(other UI Elements)
.
.
</Grid>
</mvvm:SessionStateAwarePage>
IMHO that it is perfectly fine for the wait cursor logic to be next to the command in the viewmodel.
As to the best way to do change the cursor, create a IDisposable wrapper that changes the Mouse.OverrideCursor property.
public class StackedCursorOverride : IDisposable
{
private readonly static Stack<Cursor> CursorStack;
static StackedCursorOverride()
{
CursorStack = new Stack<Cursor>();
}
public StackedCursorOverride(Cursor cursor)
{
CursorStack.Push(cursor);
Mouse.OverrideCursor = cursor;
}
public void Dispose()
{
var previousCursor = CursorStack.Pop();
if (CursorStack.Count == 0)
{
Mouse.OverrideCursor = null;
return;
}
// if next cursor is the same as the one we just popped, don't change the override
if ((CursorStack.Count > 0) && (CursorStack.Peek() != previousCursor))
Mouse.OverrideCursor = CursorStack.Peek();
}
}
Usage:
using (new StackedCursorOverride(Cursors.Wait))
{
// ...
}
The above is a revised version of the solution that I posted to this question.
private static void LoadWindow<T>(Window owner) where T : Window, new()
{
owner.Cursor = Cursors.Wait;
new T { Owner = owner }.Show();
owner.Cursor = Cursors.Arrow;
}
What's the best way to add margin between columns or rows in a WPF or Silverlight grid?
Add fixed width/height columns/rows to the grid
Add margin to the grid child controls
Anything else?
Thanks in advance
It depends, really, on your design, and is a matter of your own tastes. The biggest thing is to be consistent.
I think it's perfectly acceptable to put a fixed width "spacer" column or row in most cases - then you don't have to worry about maintenance later (either by you or somebody else).
The thing to watch out for is setting things twice (i.e. both a margin and fixed width column). It's not too big a problem if you are using all the same kind of control, but it could get a little ugly if you use different kinds of controls that have Styles applied to them that include Margins and/or Padding.
If you don't mind deriving your own control from the Grid and using that instead, you can do it quite easily. Since it seems like a good idea I quickly whipped up this (mostly untested and quite ugly!) code:
/// <summary>
/// Enhanced Grid that can automatically apply a padding to all its children.
/// </summary>
public class PaddedGrid : Grid
{
/// <summary>
/// Gets or sets a padding value to apply as the margin to all children.
/// If left to default (null or 'zero' Thickness) the margins of the children are not modified.
/// </summary>
public Thickness? Padding
{
get { return (Thickness?)GetValue(PaddingProperty); }
set { SetValue(PaddingProperty, value); }
}
public static readonly DependencyProperty PaddingProperty =
DependencyProperty.Register("Padding", typeof(Thickness?), typeof(PaddedGrid), new PropertyMetadata(PaddingChanged));
private bool HasPadding()
{
return Padding.HasValue && Padding.Value != default(Thickness);
}
private static void PaddingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var g = d as PaddedGrid;
if (g != null)
{
if (!g.HasPadding()) return;
for (int i = 0; i < g.VisualChildrenCount; i++)
{
var v = g.GetVisualChild(i);
var c = v as FrameworkElement;
if (c == null || c is GridSplitter) continue;
c.Margin = (Thickness)e.NewValue;
}
}
}
protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved)
{
base.OnVisualChildrenChanged(visualAdded, visualRemoved);
if (!HasPadding()) return;
if (visualAdded != null)
{
var fe = visualAdded as FrameworkElement;
if (fe != null) fe.Margin = this.Padding.Value;
}
}
}
I am trying to create a Blend behavior related to ComboBoxes. In order to get the effect I want, the ItemsPanel of the ComboBox has to have a certain element added to it. I don't want to do this in every ComboBox that uses the behavior, so I want the Behavior to be able to inject the ItemsPanelTemplate programmatically. However, I can't seem to find a way to do this. ItemsPanelTemplate does not seem to have a property/method that lets me set the visual tree. WPF ItemsPanelTemplate has the VisualTree but Silverlight does not.
Basically, what is the programmatic equivalent of this XAML?
<ComboBox>
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
Edit:
Okay apparently that is not an easy question, so I started a bounty and I'm going to give some more background in case there is another way to go about this. I want to provide keyboard support for the Silverlight ComoboBox. Out of the box it only supports the up and down arrows but I also want it to work so that when the user hits a letter, the ComboBox jumps to the first item of that letter, similar to how ComboBoxes work in a browser or Windows app.
I found this blog post, which got me half way. Adapting that behavior code, the ComboBox will change selection based on letter input. However, it does not work when the ComboBox is opened. The reason for this, according to this blog post is that when the ComboBox is opened, you are now interacting with its ItemsPanel and not the ComboBox itself. So according to that post I actually need to add a StackPanel to the ItemsPanelTemplate and subscribe to the StackPanel's KeyDown event, in order to take action when the ComboBox is opened.
So that is what prompted my question of how to get a StackPanel into the ItemsPanelTemplate of a ComboBox, from a behavior. If that is not possible, are there alternative ways of getting this to work? Yes, I know I could go to each ComboBox in the application and add a StackPanel and the event. But I want to do this through a behavior so that I don't have to modify every ComboBox in the app, and so I can reuse this logic across applications.
AnthonyWJones' answer below using XamlReader gets me part way, in that I can create the StackPanel and get it into the template. However, I need to be able to get at that SP programmatically in order to subscribe to the event.
This should work. I've shown how you can change the orientation below. You can add additional SetValue calls to modify other properties.
cb.ItemsPanel = new ItemsPanelTemplate();
var stackPanelFactory = new FrameworkElementFactory(typeof (StackPanel));
// Modify it like this:
stackPanelFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
// Set the root of the template to the stack panel factory:
cb.ItemsPanel.VisualTree = stackPanelFactory;
You can find more detailed information in this article: http://www.codeproject.com/KB/WPF/codeVsXAML.aspx
What you actually want to build programmatically is this:-
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
Your behaviour will then assign this to the ItemsPanel property of the ComboBox it is attached to. Currently your behaviour is pure code but there is no way to create the above purely in code.
Since this is such a small piece for of Xaml the easiest approach is to use the XamlReader:-
ItemsPanelTemplate itemsPanelTemplate = XamlReader.Load("<ItemsPanelTemplate><StackPanel /></ItemsPanelTemplate>");
I think, best way for you - extend combobox functionality not via behavior but using inheritance.
So, you can create own control MyComboBox:ComboBox. Create style for it - get default ComboBox Style
here
And write instead (look for ScrollViewer by name):
< ScrollViewer x:Name="ScrollViewer" BorderThickness="0" Padding="1" >
< ItemsPresenter />
< /ScrollViewer >
this
< ScrollViewer x:Name="ScrollViewer" BorderThickness="0" Padding="1" >
< StackPanel x:Name="StackPanel" >
< ItemsPresenter />
< /StackPanel >
< /ScrollViewer >
This StackPanel you can get in code:
public class MyComboBox: ComboBox{
public CM()
{
DefaultStyleKey = typeof (MyComboBox);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
StackPanel stackPanel = (StackPanel)GetTemplateChild("StackPanel");
stackPanel.KeyUp += (s, e) => { /*do something*/ };
}
}
Inheritance is more powerful. It's allow work with template elements.
If you decided to inject ItemsPanel, you must understand that:
1)it's impossible from code with keeping reference on injected panel.
2)to get reference to injected panel, this panel must registered itself in some storage, e.g.
< ComboBox>
< ComboBox.ItemsPanel>
< ItemsPanelTemplate>
< StackPanel>
< i:Interaction.EventTriggers>
< i:EventTrigger EventName="Loaded">
< RegisterMyInstanceInAccessibleFromCodePlaceAction/>
< /i:EventTrigger>
< /i:Interaction.EventTriggers>
< /StackPanel>
< /ItemsPanelTemplate>
< /ComboBox.ItemsPanel>
< /ComboBox>
Good luck!
I found your post while trying to set the ItemsPanel from code so that I can implement a VirtualizingStackPanel. When there are hundreds of items in my list, performance sucks. Anyway .. here's how I did it.
1) Custom control
2) Custom Behavior
-- you could also just apply this behavior to the normal ComboBox - either at each instance, or through a style.
-- you might also expose the timeout value so that can be overridden in xaml ..
-- also, it seems this doesn't work when the dropdown itself is open. not sure why exactly .. never looked into it
1..
public class KeyPressSelectionComboBox : ComboBox
{
private BindingExpression _bindingExpression;
public KeyPressSelectionComboBox()
: base()
{
Interaction.GetBehaviors(this).Add(new KeyPressSelectionBehavior());
Height = 22;
this.SelectionChanged += new SelectionChangedEventHandler(KeyPressSelectionComboBox_SelectionChanged);
}
void KeyPressSelectionComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (_bindingExpression == null)
{
_bindingExpression = this.GetBindingExpression(ComboBox.SelectedValueProperty);
}
else
{
if (this.GetBindingExpression(ComboBox.SelectedValueProperty) == null)
{
this.SetBinding(ComboBox.SelectedValueProperty, _bindingExpression.ParentBinding);
}
}
}
}
2...
/// <summary>
/// This behavior can be attached to a ListBox or ComboBox to
/// add keyboard selection
/// </summary>
public class KeyPressSelectionBehavior : Behavior<Selector>
{
private string keyDownHistory = string.Empty;
private double _keyDownTimeout = 2500;
private DateTime _lastKeyDownTime;
private DateTime LastKeyDownTime
{
get
{
if (this._lastKeyDownTime == null)
this._lastKeyDownTime = DateTime.Now;
return this._lastKeyDownTime;
}
set { _lastKeyDownTime = value; }
}
/// <summary>
/// Gets or sets the Path used to select the text
/// </summary>
public string SelectionMemberPath { get; set; }
/// <summary>
/// Gets or sets the Timeout (ms) used to reset the KeyDownHistory item search string
/// </summary>
public double KeyDownTimeout
{
get { return _keyDownTimeout; }
set { _keyDownTimeout = value; }
}
public KeyPressSelectionBehavior() { }
/// <summary>
/// Attaches to the specified object: subscribe on KeyDown event
/// </summary>
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.KeyDown += DoKeyDown;
}
void DoKeyDown(object sender, KeyEventArgs e)
{
// Create a list of strings and indexes
int index = 0;
IEnumerable<Item> list = null;
var path = SelectionMemberPath ??
this.AssociatedObject.DisplayMemberPath;
var evaluator = new BindingEvaluator();
if (path != null)
{
list = this.AssociatedObject.Items.OfType<object>()
.Select(item =>
{
// retrieve the value using the supplied Path
var binding = new Binding();
binding.Path = new PropertyPath(path);
binding.Source = item;
BindingOperations.SetBinding(evaluator,
BindingEvaluator.TargetProperty, binding);
var value = evaluator.Target;
return new Item
{
Index = index++,
Text = Convert.ToString(value)
};
});
}
else
{
list = this.AssociatedObject.Items.OfType<ListBoxItem>()
.Select(item => new Item
{
Index = index++,
Text = Convert.ToString(item.Content)
});
}
// Sort the list starting at next selectedIndex to the end and
// then from the beginning
list = list.OrderBy(item => item.Index <=
this.AssociatedObject.SelectedIndex ?
item.Index + this.AssociatedObject.Items.Count : item.Index);
// calculate how long has passed since the user typed a letter
var elapsedTime = DateTime.Now - this.LastKeyDownTime;
if (elapsedTime.TotalMilliseconds <= this.KeyDownTimeout)
{ /* if it's less than the timeout, add to the search string */
this.keyDownHistory += GetKeyValue(e);
}
else
{ /* otherwise replace it */
this.keyDownHistory = GetKeyValue(e);
}
// Find first starting with the search string
var searchText = this.keyDownHistory;
var first = list.FirstOrDefault(item =>
item.Text.StartsWith(searchText, StringComparison.InvariantCultureIgnoreCase));
if (first != null)
{ /* found */
this.AssociatedObject.SelectedIndex = first.Index;
}
else
{ /* not found - so reset the KeyDownHistory */
this.keyDownHistory = string.Empty;
}
// reset the last time a key was pressed
this.LastKeyDownTime = DateTime.Now;
}
/// <summary>
/// Gets the value of the pressed key,
/// specifically converting number keys from their "Dx" value to their expected "x" value
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
private static string GetKeyValue(KeyEventArgs e)
{
string rValue = string.Empty;
switch (e.Key)
{
default:
rValue = e.Key.ToString();
break;
case Key.D0:
case Key.NumPad0:
rValue = (0).ToString();
break;
case Key.D1:
case Key.NumPad1:
rValue = (1).ToString();
break;
case Key.D2:
case Key.NumPad2:
rValue = (2).ToString();
break;
case Key.D3:
case Key.NumPad3:
rValue = (3).ToString();
break;
case Key.D4:
case Key.NumPad4:
rValue = (4).ToString();
break;
case Key.D5:
case Key.NumPad5:
rValue = (5).ToString();
break;
case Key.D6:
case Key.NumPad6:
rValue = (6).ToString();
break;
case Key.D7:
case Key.NumPad7:
rValue = (7).ToString();
break;
case Key.D8:
case Key.NumPad8:
rValue = (8).ToString();
break;
case Key.D9:
case Key.NumPad9:
rValue = (9).ToString();
break;
}
return rValue;
}
/// <summary>
/// Helper class
/// </summary>
private class Item
{
public int Index;
public string Text;
}
/// <summary>
/// Helper class used for property path value retrieval
/// </summary>
private class BindingEvaluator : FrameworkElement
{
public static readonly DependencyProperty TargetProperty =
DependencyProperty.Register(
"Target",
typeof(object),
typeof(BindingEvaluator), null);
public object Target
{
get { return GetValue(TargetProperty); }
set { SetValue(TargetProperty, value); }
}
}
}