Detect stylus eraser button on WPF - wpf

Is there any way to detect that the stylus eraser button is pressed on a WPF element?

The answer seems to be to detect if the pen is Inverted.
http://msdn.microsoft.com/EN-US/library/ms607230(VS.110).aspx
(tested on Lenovo x61 tablet)
In MainWindow.Xaml
<TextBlock Name="textBox"
Text="Hello World!" HorizontalAlignment="Center" VerticalAlignment="Center"
StylusDown="TextBlock_StylusDown_1"/>
In MainWindow.Xaml.cs
private void TextBlock_StylusDown_1(object sender, StylusDownEventArgs e)
{
if (e.Inverted)
textBox.Text = "I've been erased";
else
textBox.Text = "Normal stylus";
}

Related

Story board for smooth touch scroll in WPF

Please provide storyboard for smooth touch scrolling for tablet in wpf. In my case touch response is not smooth and is shifting like it is going to a different page.
No need for storyBoard...
just add scrollviewer to your code
<ScrollViewer
VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Disabled" PanningMode="Both" ManipulationBoundaryFeedback="ScrollViewerCanvas_ManipulationBoundaryFeedback">
//Add stuff here
</ScrollViewer>
in codeBehind:
private void ScrollViewerCanvas_ManipulationBoundaryFeedback(object sender, ManipulationBoundaryFeedbackEventArgs e)
{
e.Handled = true;
}

WPF scrolling parent container through child container

I'm having some issues trying to figure out how to scroll the content of a grid which is contained inside of a scroll viewer. When trying to scroll with the mouse wheel or pan (with a touch screen), the grid scrolls fine if the mouse/touch point is over an empty area, but if it is above certain controls (ex. a group box) it won't scroll. Is there some property I'm missing to allow the child panels to allow them to scroll their parent containers?
EDIT:
I incorrectly stated my original layout. Here's a simplified version of my senario:
<Grid>
<ScrollViewer Name="MainScrollViewer">
<StackPanel>
<ListBox /> <--Doesn't Scroll-->
<Button /> <--Scrolls Fine-->
<TextBlock /> <--Scrolls Fine-->
<TextBox /> <--Scrolls Fine-->
<DataGrid /> <--Doesn't Scroll-->
</StackPanel>
</ScrollViewer>
</Grid>
A coworker pointed out that my issue is due to the fact the controls such as a ListBoxes and DataGrids contain ScrollViewers themselves, this makes sense. His suggestion (which would work but we both agree seems more complex than it should be) is to catch and rethrow the the scroll event in the code behind (and likely have to deal with calculating the smount of offset to scroll) so that it can bubble up to "MainScrollViewer".
EDIT 2:
It seems like the only way to achieve this is to use code behind to handle the PreviewMouseWheel event in the parent. That works, but how do I go about implementing the same thing for panning (scrolling by finger on a touch screen)?
Create a bubbling scrollbehavior for your scrollview:
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
public sealed class BubbleScrollEvent : Behavior<UIElement>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.PreviewMouseWheel += AssociatedObject_PreviewMouseWheel;
}
protected override void OnDetaching()
{
AssociatedObject.PreviewMouseWheel -= AssociatedObject_PreviewMouseWheel;
base.OnDetaching();
}
void AssociatedObject_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
if (!e.Handled)
{
e.Handled = true;
var e2 = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta) { RoutedEvent = UIElement.MouseWheelEvent };
AssociatedObject.RaiseEvent(e2);
}
}
}
Add this behavior to your scrollviewer:
<ScrollViewer x:Name="Scroller">
<i:Interaction.Behaviors>
<ViewAddons:BubbleScrollEvent />
</i:Interaction.Behaviors>
Use ScrollViewer's PreviewMouseWheel event and ScrollToVerticalOffset method...
private void ScrollViewerOnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
var scv = sender as ScrollViewer;
if (scv == null) return;
scv.ScrollToVerticalOffset(scv.VerticalOffset - e.Delta);
e.Handled = true;
}
For Starters, be sure you are using the Tech that already exists. That may resolve your issue.
<Grid HorizontalAlignment="Left" Height="300" Margin="10,10,0,0" VerticalAlignment="Top" Width="497" ScrollViewer.VerticalScrollBarVisibility="Visible" />
Although if that doesn't resolve the issue, and i know this sounds strange, Set the background color of the grid AND problem object to #00000000. (if it is not already assigned a color/brush)
<Grid HorizontalAlignment="Left" Height="300" Margin="10,10,0,0" VerticalAlignment="Top" Width="497" Background="#00000000"/>
I know its strange, but when I have these problems it works every time. I have no idea why it works. Something to do with transparency.

How to make TextChanged happen for copy/paste for TextBox in Silverlight?

For silverlight control TextBox, when copy/paste content for TextBox Text property, TextChanged Event not happening.
How to make paste same as typing for Text in TextBox?
Sorry, I can't reproduce this.
I used the following XAML
<StackPanel>
<TextBox TextChanged="TextBox_TextChanged" />
<TextBlock x:Name="tbk" />
</StackPanel>
and the following event handler
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
tbk.Text += "X";
}
Every time I typed in the TextBox or pasted text into it, the TextBlock gained another X.

VB WPF textblock.text equal a text file

In VB2010 WPF, i have a textblock which, when the window loads, i need the text inside the textblock to be the same as the text in an outside .txt file. How can i do this?
Thanks
Nick
<Window
...
Loaded="Window_Loaded">
...
<TextBlock Name="textBlock" .../>
...
</Window>
and...
private void Window_Loaded(object sender, RoutedEventArgs e)
{
textBlock.Text = System.IO.File.ReadAllText(path);
}

How to focus in WPF with WindowsFormHost

I have this simple setup:
<StackPanel>
<TextBox Text="wpf1" PreviewLostKeyboardFocus="TextBox_PreviewLostKeyboardFocus" />
<TextBox Text="wpf2" PreviewLostKeyboardFocus="TextBox_PreviewLostKeyboardFocus" />
<WindowsFormsHost>
<wf:TextBox Text="winforms" />
</WindowsFormsHost>
</StackPanel>
private void TextBox_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
e.Handled = true;
}
Three textboxes, two WPF and one WinForm. I can't move focus between the two WPF-Textboxes which I desired but I can move focus to the WinForm-Textbox. The event PreviewLostKeyboardFocus does not even triggered when moving from a WPF-Textbox to a WinForm-Textbox. Any clues to why and how this could be solved?
EDIT
I've noticed that WindowsFormsHost.PreviewGotKeyboardFocus is triggered first when focus is leaving the WindowsFormsHost again. Thats odd. Maybe it's a bug?

Resources