ListView Item Selection Windows Store - c#

I want a ListView to behave like this:
With mouse Input:
Left Click -> Item click event handler gets executed, but should not
display it as "selected"
Right Click -> Item gets selected
With touch Input:
Single Tap -> equivalent to left click
Swipe Down -> equivalent to right click
I have played around with various of the events and settings but cant seem to get it to work right.

In other words, you want your listview to behave like the Windows Start screen? This one was brutal for me to figure out - the mouse part was easy, but the touch part not so much. The solution turns out to be pretty easy. You just have to enable the right options for your listview. Here's the xaml for mine:
<ListView
x:Name="itemListView"
SelectionMode="Extended"
IsSwipeEnabled="True"
IsItemClickEnabled="True"
ItemClick="ItemView_ItemClick"
/>
Sorry, I haven't figured how to get code to highlight in StackOverflow yet.

This could help you with the mouseclicks
private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
method()
if (e.Button == MouseButtons.Right)
set selection = false
method()
}
and for the handle of the touch i hope this helps
http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh465387.aspx

Related

Adding inertia for WPF touch application listbox

I have a full screen WPF application built for a touch monitor, and I have some ListBoxes on the main screen.
When I flick the Listbox it scrolls fine, but when it gets to the end of the list, the entire application gets pulled down from the top of the screen, But I need the inertia just for the list box not for the entire window. How I can achieve that?
The ManipulationBoundaryFeedback event enables applications or
components to provide visual feedback when an object hits a boundary.
For example, the Window class handles the ManipulationBoundaryFeedback
event to cause the window to slightly move when its edge is
encountered.
So, a way around it is to handle ManipulationBoundaryFeedback on the ListBox, and set Handled to true:
<ListBox ManipulationBoundaryFeedback="OnManipulationBoundaryFeedback">
// ...
</ListBox>
Code-behind:
private void OnManipulationBoundaryFeedback(object sender, ManipulationBoundaryFeedbackEventArgs e)
{
e.Handled = true;
}

Remap left click to right click c#

Hey folks having issues finding any info on how I might remap the left click event in C# to be a right click event.
For example, everytime the user left clicks on a DataGrid in my application I want to be able to capture the left click and map it to a right click. So how do I simulate a right click every time the left click on the mouse is used. I've found some old .net2.0 examples where some C++ was ported over to C# to allow simulating clicks but I don't think it would be suitable and I don't truly understand what it was doing.
Any help appreciated. Thanks!
Just handle the mouse click event of the Datagrid
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
contextMenuStrip1.Show();
}
else if (e.Button == MouseButtons.Right)
{
//What you want it to do
}
}
When handling the event, check if it is a right click or a left one. No need to map the mouse clicks.

C# WinForms Right Click Context Menu without Selecting item

I have a right click menu on c# winforms and the question is when I right click to get the menu how do I not get it to select the item when I right click?
I found a similar question, but it is for WPF here at this link
The problem being that i have index_changed event that is affected by the right click and i don't want it to be.
I am guessing that you want this to be handled in the Listviews selected index changed event..... The sample below shows how to determine the button pressed. Al it does is tell you yes or no if the right mouse button was clicked. This is the simplest example I can show without knowing your code.
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(Form.MouseButtons.HasFlag(MouseButtons.Right).ToString());
}

WPF ScrollViewer

I was wondering if there is anyway to check if the up arrow or the bottom arrow of a wpf scroll viewer is clicked.
I am trying to do it within a wpf textbox but, I want it to snap to the next line of a text instead of displaying partial text.
So, the way for me to do this is when up/ or down is clicked.
i would say
textBox.lineup/linedown.
but I also need to know which component is clicked in order to do so.
Thanks in advance!
-Kevin
You can use ScrollChanged event in ScrollViewer as below
<ScrollViewer ScrollChanged="ScrollViewer_ScrollChanged">
In code you can get the verticalOffSet value.
private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
double verticalOffSet = e.VerticalOffset;
...
}

WPF create a list of controls that can be scrolled via the mouse but still remain functional

I have a list of controls that I am displaying via a WrapPanel and it horizontally oriented.
I have implemented a "Click and Drag" scrolling technique so that the user scrolls with the mouse via clicking and dragging.
Like so:
<Canvas x:Name="ParentCanvas" PreviewMouseDown="Canvas_MouseDown" MouseMove="Canvas_MouseMove">
<WrapPanel>
<WrapPanel.RenderTransform>
<TranslateTransform />
</WrapPanel.RenderTransform>
<!-- controls are all in here ... -->
</WrapPanel>
</Canvas>
Then in the code behind:
private Point _mousePosition;
private Point _lastMousePosition;
private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
_lastMousePosition = e.GetPosition(ParentCanvas);
e.Handled = true;
}
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
_mousePosition = e.GetPosition(ParentCanvas);
var delta = _mousePosition - _lastMousePosition;
if(e.LeftButton == MouseButtonState.Pressed && delta.X != 0)
{
var transform = ((TranslateTransform)_wrapPanel.RenderTransform).Clone();
transform.X += delta.X;
_wrapPanel.RenderTransform = transform;
_lastMousePosition = _mousePosition;
}
}
This all works fine
But what I want to do is make it so that when a users clicks to drag, the items within the WrapPanel dont respond (i.e. the user is only browsing), but when the user clicks (as in a full click) then they do respond to the click.
Just like how the iphone works, when you press and drag directly on an app, it does not open the app, but rather scrolls the screen, but when you tap the app, it starts...
I hope this makes sense.
Cheers,
Mark
I believe you'll need to capture the mouse. The problem is you'll be contending with the controls (such as Button) that will also be trying to capture the mouse.
In your MouseDown event (probably PreviewMouseDown actually) you can use e.MouseDevice.Capture(_wrapPanel, CaptureMode.Element). This should direct all mouse input to the _wrapPanel and not any subtree elements.
In your MouseUp event, you'll need to release the capture by calling e.Mousedevice.Capture(null). If no scrolling has taken place you'll want to send a "click" to the control that normally would have received the click which I'm not quite sure about. Perhaps you can use the Automation Peer classes to do this?
The trick is that certain controls will not work properly if you withhold mouse events from them. Consider a slider for example. How would the slider ever be usable inside a panel that works like this?
Another, and in my opinion better, solution is to:
Add a PreviewMouseDown handler in which you set Handled=true and record the parameters including the position and set a "maybeClick" flag (unless your "recursion" flag is set), and sets a timer.
Add a MouseMove handler that clears the "maybeClick" flag if the mouse moves more than an epsilon away from the position recorded for the PreviewMouseDown.
Add a PreviewMouseUp handler that checks the "maybeClick" flag - if true, it sets the "recursion" flag, does an InputManager.ProcessInput to re-raise the original PreviewMouseDown, and then clears the "recursion" flag.
In the timer, do the same thing as for PreviewMouseUp so the click will only be delayed a moment.
The net effect is to delay a PreviewMouseDown event until you have had time to check whether the mouse moved or not.
Some things to note about this solution:
Setting Handled=true on PreviewMouseDownEvent also stops the MouseDownEvent.
The recursive call is ignored in the PreviewMouseDown handler because the recursion flag is set

Categories

Resources