Issue with scroll viewer in windows 10 universal app? - c#

I am working on a windows 10 universal App.The basic structure of my page is like
<Grid>
<ScrollViewer ViewChanged="MyScrollViewer_ViewChanged" VerticalScrollBarVisibility="Auto" VerticalScrollMode="Enabled" HorizontalScrollBarVisibility="Disabled" HorizontalScrollMode="Disabled" IsVerticalRailEnabled="True" IsVerticalScrollChainingEnabled="True">
<Pivot>
<PivotItem/>
<PivotItem/>
<PivotItem/>
<PivotItem/>
</Pivot>
</ScrollViewer>
</Grid>
And in my code behind i wrote the event to handle Scroll changed like this
private void MyScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
//My business logic goes here
}
For each pivot item, there is contents to scroll.
When i run the App what happens is
when i tried to scroll from the scroll bar on the right side,my MyScrollViewer_ViewChanged event getting fired but when i tried to scroll with the mouse wheel,the event not getting fired and i was not able to continue.
Please help to find out the issue.
Thanks

The pivot inside your scrollviewer is intercepting your touch and mouse wheel interactions, that is why you are not receiving anything with your mouse wheel.
Placing a pivot within a scrollviewer is really really weird. What behavior are you trying to achieve ? If you want to scroll vertically in each of the pivot items you should place a scrollviewer in each of your PivotItems, not around your pivot.

I had the same problem i fixed it like this but i hope we find a better way cause it buggy.
private void PivotItem_PointerWheelChanged(object sender, PointerRoutedEventArgs e)
{
if (e.GetCurrentPoint(scrollViewer).Properties.MouseWheelDelta == (-120))
{
// On Mouse Wheel scroll Backward
scrollViewer.ChangeView(null, scrollViewer.VerticalOffset + Window.Current.CoreWindow.Bounds.Height / 10, null, false);
}
if (e.GetCurrentPoint(scrollViewer).Properties.MouseWheelDelta == (120))
{
// On Mouse Wheel scroll Forward
scrollViewer.ChangeView(null,scrollViewer.VerticalOffset - Window.Current.CoreWindow.Bounds.Height / 10, null, false);
}
}

Related

Buttons to appear around click position of mouse

I'm creating a simple WPF app in which one of the core features would be that wherever the user clicks on the grid inside the main window, a number of buttons should appear around the position of the click.
Now, I try to achieve this with only 1 button. I know that I have to capture the current position of the mouse and then modify the 4 arguments of the Margin of the button (left, top, right, bottom) by creating new instances of Thickness-es.
I managed to create new Thickness-es to the Margins, with the left and top argument set to the mouse X and Y cordinates respectively, but I don't know how to calculate or what to use as the right, and bottom arguments of the newly created Margins.
Here is the relevant function from the xaml.cs (the values in question are indicated as 0-s and grid is intented to refer to the grid):
private void Grid_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
var mouseLocation = PointToScreen(Mouse.GetPosition(grid));
RandomButton.Margin = new Thickness(mouseLocation.X, mouseLocation.Y, 0, 0);
}
Here is the relevant part of the xaml:
<StackPanel>
<Button
Name="RandomButton"
Height="30"
Width="30"
Background="#FF130889"
Click="RandomButton_Click"
Content="RandomContent" />
</StackPanel>
It is also worth mentioning that when the button's HorizontalAlignment is set to Left and the VerticalAlignment is set to top, the button seem to do what I want with this setup, but only, when the windowsize is full.
I think I have to use the actual height of the window or the grid, but I don't know how. I know it is something simple, but I just started working with WPF, so I apreciate any kind of help!
As far as I understood the problem is in determining the click relative position.
In this case you can use Mouse.GetPosition method.
Here is the example with Canvas:
private void SetPos()
{
var relativePosition = Mouse.GetPosition(this.MainCanvas);
Canvas.SetLeft(this.btn1, relativePosition.X);
Canvas.SetTop(this.btn1, relativePosition.Y);
btn1.Visibility = Visibility.Visible;
}

Missing 'Text Cursor Moved' and 'Scrollbar Adjusted' events in .NET Scintilla component?

Like the RichTextBox, I want to be able to handle events for when the vertical scrollbar has been adjusted (through slider dragging, the mouse wheel or otherwise), and for when the caret / text cursor has been moved. However, these events appear to be missing from Scintilla. How can I achieve the same result?
Both of those are available under the UpdateUI event via the UpdateChange structure. Example:
private void scintilla1_UpdateUI(object sender, ScintillaNET.UpdateUIEventArgs e)
{
if (e.Change == ScintillaNET.UpdateChange.VScroll)
{
...
}
}

Wpf Mouse Events outside the bounds of a user control issue

I have a weird issue I have no ideea how to fix.
I'm creating a color picker from scratch. I'm doing so by creating a set of user controls and putting them together in a "master control".
When a user drags over the hue picker, for example, I handle the mouse down, move and up (inside the hue picker user control).
A simple bool dictates what happens, as far as the actual moving goes.
//Mouse down
_isDrag = true;
//Mouse Move
if(!_isDrag) return;
//Moving the position indicator shape thingy
//Calculating the hue
//Mouse Up
_isDrag = false;
But, if the mouse up occurs outside of the bounds of the hue picker, the mouse up event doesn't fire.
Thus, when the user returns to the area of the hue picker, the shape indicator thingy runs rampart.
I am certain an answer lies somewhere but I'm afraid my searching skills are not up to the task.
I've no idea what to look for.
Thank you for your time.
Solution:
private bool _isDrag;
//Request Mouse capture for the Container
private void MsDown(object sender, MouseButtonEventArgs e)
{
_isDrag = true;
Mouse.Capture(MainContainer);
}
//Release Mouse capture
private void MsUp(object sender, MouseButtonEventArgs e)
{
_isDrag = false;
Mouse.Capture(null);
}
//Move the handle vertically along the main container, with respect to it's width - so it's centered.
private void MsMove(object sender, MouseEventArgs e)
{
if (!_isDrag) return;
Canvas.SetTop(Handle, e.GetPosition(ContentRow).Y - Handle.ActualHeight / 2);
}
Thank you for your answer!
Edit 2:
Following up on my issue. While Capture basically did the trick, I noticed that, if fast dragging outside the bounds of the user control , sometimes the handle would get stuck close to the edge. If I would move the mouse slowly, this wouldn't happen. Weird. Also, I could never reach 0 and .ActualHeight
So I'll post my fix here, just in case another dude encounters this issue.
I split my grid like this:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="7"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="7"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="7"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="7"></RowDefinition>
</Grid.RowDefinitions>
With 7 being half the size of my handle (a circle).
The content area (the actual area you can interact with visually) is in the middle cell (in a separate grid with false hit test visibility).
Spanning the entire main grid is an invisible rectangle used for hit testing.
And to move the handle
private void MoveHandle()
{
_pos.X = _pos.X - Handle.ActualWidth/2;
_pos.Y = _pos.Y - Handle.ActualHeight / 2;
//this is just to be sure. I'm paranoid. Being a color picker, these actually matter a lot.
_pos.X = Math.Max(Math.Min(_pos.X, RectColor.ActualWidth - Handle.ActualWidth/2), -Handle.ActualWidth / 2);
_pos.Y = Math.Max(Math.Min(_pos.Y, RectColor.ActualHeight -Handle.ActualWidth/2), -Handle.ActualHeight/2);
Canvas.SetLeft(Handle, _pos.X);
Canvas.SetTop(Handle, _pos.Y);
}
I have no idea why the previous code I had almost worked. It's basically the same thing as before. But, somehow, this performs a million times better. Good luck!
The search term you are looking for is Mouse Capture. Capture in your MouseDown, and you can get mouse events even after the mouse leaves your control.

Pinch Zoom with ScrollViewer

I'm using an ScrollViewer in the MVVM enviroment to navigate around an map of europe. But when I use the ScrollViewer the deltaScale for the manipulationDeltaEventArgs.Pinchmanipulation doesn't work. The DeltaScale stays at one, no matter what. I tried to take a look at the Current and Original of the Pinchmanipulation and they are the same. So can anyone help me with making it possible to zoom while having an scrollViewer?
The manipulationDelta is:
public void Zoom(ManipulationDeltaEventArgs e)
{
if (e.PinchManipulation == null)
{
return;
}
}
I don't think scrollviewer supports zooming in Windows Phone 8. Only Windows Store apps can do this right now.

Hide scrollbars of a RichTextBox

I'm trying to write a simple text editor like DarkRoom with just a RichTextBox (or alternatively a TextBox) in it. My problem is that I can't use the mouse wheel for scrolling unless I have a vertical scrollbar. Is there any way to hide this scrollbar and still be able to scroll with the mouse wheel?
So far I have several ideas how this could be done, but no idea how to implement them.
re-create the scrolling code using a MouseWheel event
change the visual style of the scrollbar to hide it or make it less visible
write my own TextBox widget
overlap the scrollbars with something else to hide them
P.S.: Using any win32 stuff is not an option.
Yes, you will have to capture the .MouseWheel and .MouseMove events. See this post.
Ok, do something like following:
Add a line in form load event.
private void Form1_Load(object sender, EventArgs e)
{
this.richTextBox1.MouseWheel += new MouseEventHandler(richTextBox1_MouseWheel);
}
Add following in mouse wheel event.
void richTextBox1_MouseWheel(object sender, MouseEventArgs e)
{
if (e.Delta > 0)
{
//Handle mouse move upwards
if (richTextBox1.SelectionStart > 10)
{
richTextBox1.SelectionStart -= 10;
richTextBox1.ScrollToCaret();
}
}
else
{
//Mouse move downwards.
richTextBox1.SelectionStart += 10;
richTextBox1.ScrollToCaret();
}
}
Let me know in either cases, if you would want the running sample of the same; or if you are not liking the solution (0:

Categories

Resources