I have a Canvas (not InkCanvas) inside a Scrollviewer. Both are not created in XAML but in code behind. I want to draw lines on my Canvas with Pen and Mouse input, everything works just fine but now I tested the whole thing with a pen as input device and the Scrollviewer seems to recognize it as touch input because the whole thing starts scrolling.
My question is: Is it possible to tell the Scrollviewer to ignore all inputs from a device type? Because it also seems like the Scrollviewer is 'eating' the events which should be fired from the Canvas.
Here my Scrollviewer init:
private void SetUpScrollViewer()
{
scroll = new ScrollViewer();
scroll.VerticalScrollMode = ScrollMode.Auto;
scroll.HorizontalScrollMode = ScrollMode.Auto;
scroll.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
scroll.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
scroll.ZoomMode = ZoomMode.Enabled;
scroll.ManipulationMode = ManipulationModes.System;
scroll.HorizontalAlignment = HorizontalAlignment.Left;
scroll.VerticalAlignment = VerticalAlignment.Top;
scroll.IsZoomInertiaEnabled = false;
scroll.MinZoomFactor = 1;
scroll.MaxZoomFactor = 5;
}
Those are the events I use in my Canvas:
public void EnableDrawingOnCanvas(Canvas canvas)
{
//Adding the needed event handler.
canvas.PointerPressed += Canvas_PointerPressed;
canvas.PointerMoved += Canvas_PointerMoved;
canvas.PointerReleased += Canvas_PointerReleased;
canvas.PointerExited += Canvas_PointerExited;
}
And those events all check if the input device is everything but touch like this
if (e.Pointer.PointerDeviceType != Windows.Devices.Input.PointerDeviceType.Touch){...}
But with those Events I can only check the input device for my Canvas and if I add a Event to the Scrollviewer it won't get passed to the Canvas afaik.
You can bind a PointerPressed event to your ScrollViewer and check if the e.Pointer.PointerDeviceType equals PointerDeviceType.Pen. Then you can disable the VerticalScrollMode the HorizontalScrollMode and the ZoomMode like in the code below.
If you want to reactivate the ScrollViewer you can bind a PointerExited event to your ScrollViewer and reenable everything.
private void Scroll_PointerPressed(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerDeviceType == PointerDeviceType.Pen)
{
scroll.VerticalScrollMode = ScrollMode.Disabled;
scroll.HorizontalScrollMode = ScrollMode.Disabled;
scroll.ZoomMode = ZoomMode.Disabled;
}
}
Related
I use a WPF to display a shape on an Office panel.
I can size the shape when application is loading. But I would like to resize this shape if the user resize the panel containing the shape.
My problem is margin defined at the beginning is not changed after loading, so the size keep its initial margin.
I have a UserControl containing the WPF Shape and the resize event handler:
private void UserControlA_Resize(object sender, EventArgs e)
{
myWPF.SetSizeOfShape(sizeOfPanel); // I collect sizeOfPanel, and it is OK, it is changed when panel is resized
}
xaml of the WPF is:
<Border Name="myShape" Background="blue" CornerRadius="8" Margin="10,126,139,199" />
the WPF .cs is:
public void SetSizeOfShape(int widthOfPanel)
{
myShape.Margin = new Thickness(widthOfPanel/3, 100, widthOfPanel/6, 100);
SetSizeOfShape is called when the application is loading and the size is correctly set - but if size is changed, it is called again, but doesn't change the margin displayed.
Do you know what is wrong and how to correct it ?
----- EDIT -----
There is probably a problem with my event handler. Indeed, if I put the event with a button click, it works - but if I use the Resize (or sizeChanged) event it doesn't : the event is called, but there is no effect on the shape. Do you know how to solve it ?
I don't see a Resize event for the Border Control. It can size to its contents. Maybe give us more detail about what you're trying to do.
public void MyMouseOver()
{
Ellipse myShape = new Ellipse() { Width = 200, Height = 100, Stroke = Brushes.Yellow, };
MyCanvas.Children.Add(myShape);
Canvas.SetTop(myShape,10);
Canvas.SetLeft(myShape,10);
myShape.MouseEnter += MyShape_MouseEnter;
myShape.MouseLeave += MyShape_MouseLeave;
}
private void MyShape_MouseLeave(object sender, MouseEventArgs e)
{
((Ellipse)sender).RenderTransform = new ScaleTransform(1, 1); // return scale to normal
}
private void MyShape_MouseEnter(object sender, MouseEventArgs e)
{
((Ellipse)sender).RenderTransform = new ScaleTransform(1.1, 1.1, Width / 2, Height / 2);
}
I have a simple ListView in a Universal Windows App. It scrolls just fine both on a touch-enabled device and on my local machine. On the touch device I would like to be able to tap and drag the scroll bar thumb in order to quickly scroll through the list (same as you would get clicking and dragging the scroll thumb with a mouse). But, when I try to select the scroll bar thumb it does not work; the thumb is not selectable.
At first I thought the scroll thumb was just too small, so I fiddled with the default style of ScrollBar to increase the width. Then I tried tweaking other values in the default ScrollViewer style, like the ScrollingIndicatorMode and ScrollingIndicatorStates so that they would always use MouseIndicator, and making sure all IsHitTestVisible are True. To no avail.
It seems like there must be something in the default styles that allows this but I can't find it through trial and error, and no where in the MSDN docs appears to address this styling.
Is this doable in touch mode?
Is this doable in touch mode?
AFAIK, this is not possible currently. The scroll event of ScrollBar can only be triggered by scrolling the content or moving the Thumb by mouse input.
This is by design, but on the touch device if we want to quickly scroll through the list, we just quickly swipe one item for a short distance, the ListView will firstly speed up the scrolling and then slow down and finally stop. Other controls which contain a ScrollViewer behave the same, in the viewport(content of ScrollViewer) when in touch device, moving the thumb is not work, to speed up the scrolling, you can only give quick swipe gesture on the viewport.
Our suggestion is that you may submit a request to add this new features for developing through the Windows Feedback tool.
For posterity's sake I want to post my work around which actually turned out not being quite as difficult as I thought and looks really good now.
The basic idea is to use a Slider control (has a Thumb that is select-able, drag-able, and moves along a track) with a vertical orientation and to sync the Slider's Value property (which changes when dragged) to the ListView's ScrollIntoView method so you can scroll the ListView as the Slider Thumb is dragged. Vice-versa (move the Slider Thumb when the ListView scrolls normally) is also required for a clean, seamless experience. I have some code sample below to get you started (not everything is there to work right away). In addition, edit the Slider's default Style template to make it look exactly like a scroll bar (and even even add a tooltip to display the current value when dragging).
Note: I'm using WinRTXamlToolkit in code-behind to traverse the VisualTree easily.
Code behind:
public sealed partial class MainPage : Page
{
private bool _isScroll = false;
private bool _isSlide = false;
public MainPage()
{
this.InitializeComponent();
var vm = new ViewModel();
vm.ValueChanged += LetterSliderValueChanged;
DataContext = vm;
}
/// <summary>
/// Bring list items into view on the screen based on the value (letter) of the slider
/// </summary>
/// <param name="sender">The view model to bring into view</param>
private void LetterSliderValueChanged(object sender, RoutedEventArgs e)
{
if (_isScroll) return;
if (sender == null) return;
_isSlide = true;
ListView?.ScrollIntoView(sender, ScrollIntoViewAlignment.Leading);
}
/// <summary>
/// Update the position of the slider when the ListView is scrolling from a normal touch
/// </summary>
private void ScrollViewerViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
if (_isSlide)
{
_isSlide = false;
return;
}
_isScroll = true;
var scrollViewer = sender as ScrollViewer;
var scrollBars = scrollViewer.GetDescendantsOfType<ScrollBar>();
var verticalBar = scrollBars.FirstOrDefault(x => x.Orientation == Orientation.Vertical);
// Normalize the scales to move the slider thumb in sync with scrolling
var sliderTotal = LetterSlider.Maximum - LetterSlider.Minimum;
var barTotal = verticalBar.Maximum - verticalBar.Minimum;
var barPercent = verticalBar.Value / barTotal;
LetterSlider.Value = (barPercent * sliderTotal) + LetterSlider.Minimum;
_isScroll = false;
}
/// <summary>
/// Add the slider method to the ListView's ScrollViewer Viewchanged event
/// </summary>
private void ListViewLoaded(object sender, RoutedEventArgs e)
{
var listview = sender as ListView;
if (listview == null) return;
var scrollViewer = listview.GetFirstDescendantOfType<ScrollViewer>();
scrollViewer.ViewChanged -= ScrollViewerViewChanged;
scrollViewer.ViewChanged += ScrollViewerViewChanged;
}
}
XAML:
<Slider x:Name="LetterSlider" Orientation="Vertical"
Value="{Binding SliderValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Maximum="{Binding MaxSliderLetter, Mode=OneTime}"
Minimum="{Binding MinSliderLetter, Mode=OneTime}"
RenderTransformOrigin="0.5,0.5">
<Slider.RenderTransform>
<RotateTransform Angle="180"/>
</Slider.RenderTransform>
</Slider>
View Model:
//Method to update the ListView to show items based on the letter of the slider
public RoutedEventHandler ValueChanged;
public int SliderValue
{
get { return _sliderValue; }
set
{
_sliderValue = value;
NotifyPropertyChanged("SliderValue");
char letter = (char)_sliderValue;
var items = ItemsGroup.FirstOrDefault(i => (char)i.Key == letter);
if (items == null) return;
ValueChanged(items.FirstOrDefault(), new RoutedEventArgs());
}
}
Maybe I'm misunderstanding how this is supposed to work, but from everything I've read it's my understanding that setting the IsHitTestVisible property to false should essentially make that element "invisible" to mouse events, and MSDN states that this property "declares whether this element can possibly be returned as a hit test result".
I have a procedurally generated Grid, each cell contains a Border, and each Border's child is a TextBlock.
This is how I'm creating the cell:
var cellBorder = new Border { BorderBrush = Brushes.LightGray, Background = Brushes.WhiteSmoke, BorderThickness = thickness };
var label = new TextBlock { Text = time.ToShortTimeString(), Foreground = Brushes.Tomato, IsHitTestVisible = false };
cellBorder.Child = label;
_grid.Children.Add(cellBorder);
Grid.SetColumn(cellBorder, j);
Grid.SetRow(cellBorder, i);
In an DragMove event handler, I want to change background color of the current cell. This was working fine before I added the TextBlock to the border. It looks something like this:
void _grid_DragOver(object sender, DragEventArgs e)
{
var pos = e.GetPosition(_grid);
var result = VisualTreeHelper.HitTest(_grid, pos);
if (result != null)
{
var border = result.VisualHit as Border;
if (border != null)
border.Background = Brushes.LightYellow;
else if (result.VisualHit is TextBlock)
Console.WriteLine("Textblock hit"); // Why is this happening?
}
}
The TextBlock is getting returned from the hit test. Why?
The VisualTreeHelper does not take IsHitTestVisible into account.
If you want to omit the TextBlock from the hit test for VisualTreeHelper then you should pass filter callbacks to HitTest()
I'm currently capturing the PointerMoved event on the page to use with a horizontal menu. So the user can swipe left/right and the page will animate accordingly.
This works when the user touches a static element (TextBlock etc.) but if they touch a ListView it captures the touch events.
How can I implement the ListView so when the user scrolls vertically it works as normal, but when the user scrolls horizontally it passes the events to my code?
It is possible, but you will need a small trick. As a refference I put here Rob Caplan's article.
Let's start:
First - where are your events? - answer is simple - while you have ScrollViewer enabled, all events are intercepted by it and handeled. You ListView will get only PointerEntered event and just after it PointerExited, all further proccesing is handeled by ScrollViewer. That is the problem. But as I've said there is a method to do what you want.
For this purpose lets assume that you have defined your ListView only with VerticalScroll:
<ListView Name="myList" ScrollViewer.HorizontalScrollMode="Disabled">
Of course it is possible to do for both directions, but it's a simple example.
Now let's have a look at constructor of a Page:
PointerPoint firstPoint = null;
ScrollViewer listScrollviewer = null;
public MainPage()
{
this.InitializeComponent();
myList.ItemsSource = yourItemSource;
myList.PointerEntered += myList_PointerEntered;
myList.PointerMoved += myList_PointerMoved;
}
Nothing weird here - I just subscribe to events, and declare two variables firstPoint and listScrollviewer, which I'll need later.
We will need also to get our ScrollViewer of our ListView - the following method will do the job:
public static ScrollViewer GetScrollViewer(DependencyObject depObj)
{
if (depObj is ScrollViewer) return depObj as ScrollViewer;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
var result = GetScrollViewer(child);
if (result != null) return result;
}
return null;
}
Now - to enable our events we will need to disable the ScrollViewer:
private ScrollViewer DisableScrolling(DependencyObject depObj)
{
ScrollViewer foundOne = GetScrollViewer(depObj);
if (foundOne != null) foundOne.VerticalScrollMode = ScrollMode.Disabled;
return foundOne;
}
We will disable the ScrollViewer upon PointerEntered event which is fired. In this step we will also remember the pressed PointerPoint - as we have disable Scrollviewer, we will have to scroll it manually - that is what we need this PointerPoint for.
private void myList_PointerEntered(object sender, PointerRoutedEventArgs e)
{
firstPoint = e.GetCurrentPoint(myList);
if (listScrollviewer == null) listScrollviewer = DisableScrolling(myList);
}
Finally our PointerMoved event, which now wil be fired as we had disabled ScrollViewer - moving ScrollViewer + other code you need to put there:
private void myList_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (listScrollviewer != null)
{
PointerPoint secondPoint = e.GetCurrentPoint(myList);
double verticalDifference = secondPoint.Position.Y - firstPoint.Position.Y;
listScrollviewer.ChangeView(null, listScrollviewer.VerticalOffset - verticalDifference, null);
}
// some other code you need
}
Few remarks:
this method still needs much tuning, but hopefuly will show you how to achieve your goal,
you may need also to separate some small horizontal movements from vertical ones,
if your ListView or other Control has horizontal scroll, then you will also need to disable and handle it,
this method won't probably work so smooth like original ScrollViewer.
I've also put a simple working example here at OneDrive.
I have some Panel's and each Panel has a PictureBox and a Label in it.
public static void redPanel(Panel panel1)
{
panel1.MouseEnter += new EventHandler((o, a) => panel1.BackColor = Color.Brown);
panel1.MouseDown += new MouseEventHandler((o, a) => panel1.BackColor = Color.DarkRed);
panel1.MouseUp += new MouseEventHandler((o, a) => panel1.BackColor = Color.Firebrick);
panel1.MouseLeave += new EventHandler((o, a) => panel1.BackColor = Color.Firebrick);
}
I made this code so the panel reacts to mouse hover and click ... but if I hover the Label or the PictureBox the Panel reverts to its original color. I want that the Label and the PictureBox to act exactly like the Panel (ex: If I click the Label I want that to count as a click on the Panel).
I want some code ... like SourceControl or something for the function from top to manage its own Label and PicureBox.
P.S: I tried to make another functions for Label and PictureBox but each Panel has different color so It means that I need to have about 23 lines of code just for labels ...
Quick answer: just attach the same handler to all of them.
public static void redPanel(Panel panel1)
{
var mouseEnter = new EventHandler((o, a) => panel1.BackColor = Color.Brown);
panel1.MouseEnter += mouseEnter;
pictureBox1.MouseEnter += mouseEnter;
label1.MouseEnter += mouseEnter;
// And so on
}
Better answer: change your code, if it's a reusable component you'd better to create a UserControl with a PictureBox and a Label. From Designer then assign the same event handler to events generated from both of them (plus UserControl itself).
That said a button is much more than that (it has to react to keyboard events, can be focused, can be default button and so on). I'd prefer to derive from button to just add drawing features (a label and an image are very little code).