I'am just coding some global gesture handlers for a wpf application. For example I am supposed to use right click as a trigger to proceed UI.
<Window.InputBindings>
<MouseBinding MouseAction="RightClick" Command="NavigationCommands.NextPage"/>
</Window.InputBindings>
But now the problem appears that <ListBox/> consumes all mouse button events. I did some research but didn't found an easy way to make it just be unaware of the right button. Did anybody ever had this problem and found a solution? Thanks in advance.
You should be able to prevent the ListBox from consuming the mouse clicks using the following:
<ListBox>
<ListBox.InputBindings>
<MouseBinding MouseAction="RightClick" Command="ApplicationCommands.NotACommand" />
</ListBox.InputBindings>
</ListBox>
Related
I have a ListBoxItem with a LeftClick Gesture:
<ListBoxItem x:Name="ListViewItemMenu" Content="{Binding Path=Header}" Padding="37 14" FontSize="15" Foreground="White">
<ListBoxItem.InputBindings>
<MouseBinding Gesture="LeftClick" Command="ApplicationCommands.New" />
</ListBoxItem.InputBindings>
</ListBoxItem>
But actually it's not a real Click, it's simply a MouseLeftButtonDown Gesture. The Command is executed regardless of whether the button is raised or not after pressed.
I would like to have a complete Click with the MouseDown and MouseUp Gestures in the ListBoxItem. There is anyway to do that? Much appreciated.
Also I can't find a way to add a Command to an Expander. The method shown above to the ListBoxItem doesn't work. Any magic here? Thanks!
According to this answer you can inherit from MouseGesture to create custom gestures. There is even a full example with implementation.
An alternative is to use an System.Windows.Interactivity.EventTrigger as shown in this answer, which would let you turn the MouseLeftButtonUp or PreviewMouseLeftButtonUp event into a command trigger.
I am wondering if there is a way to bind the current ContextMenu item to a property in WPF without first clicking on it.
For example:
<MenuItem MaxHeight="20"
HorizontalAlignment="left"
Header="Radio Group"
Name="cmRadio"
ItemsSource="{Binding RadioGroups, Mode=OneWay}" />
I'm hoping to be able to figure out what item the mouse is over before clicking.
Sorry for the lack of description ahead of time.
There are lots of "mouseover" events you could use, some totally in XAML.
Similar Problem
See farther down for the XAML answer.
I want to solve a seemingly simple task. I want to create a list of text entries where each entry is selectable (and causes navigation to another page) and when the user holds his finger over an item I want a context menu with a single option to delete that item. This is very common pattern in WP applications. For example the browser does this with favourites.
Right now I have a listbox with a textblock in the item template and I start the navigation in the SelectionChanged event:
<ListBox Name="lbSnippets" SelectionChanged="lbSnippets_SelectionChanged">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"></TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
I can think of several ways to solve the hold problem but none of them sits well with me. For example I may handle the Hold event on the TextBlock but then I will have to dig for the item related to this TextBlock. Something tells me that there should be a better way to do this as it is so common. What is the right way to solve this task?
The Silverlight toolkit for WP7 includes a ContextMenu control.
You can install the toolkit via nuget: PM> Install-Package SilverlightToolkitWP
Then you an add ContextMenus to basically any control:
<DataTemplate>
<TextBlock Text="{Binding}">
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="Delete"
Command="{Binding YourDeleteCommand}"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</TextBlock>
</DataTemplate>
Where toolkit is an xml namespace:
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
You can start learning about the ContextMenu control from this article:
WP7 ContextMenu in depth | Part1: key concepts and API
We have implemented a simple InputBinding for mouse click gestures, the code is something like below:
<Image.InputBindings>
<MouseBinding MouseAction="LeftClick" Command="{Binding OpenDialogCommand}" />
<MouseBinding MouseAction="LeftDoubleClick" Command="{Binding OpenDialogCommand}" />
</Image.InputBindings>
We expected that WPF is able to recognize Left and LeftDouble gestures and do the respective command. But in practice we found that the Left Click is evaluated first and the second click of the double click is treated as another single click. Since our command is to open a dialog doing a doubleclick will quickly open and close our dialog.
Does anyone meet such thing before?
Thanks.
S.
I think this is the desired behavior. Note that it is a LeftClick event, which doesn't necessarily mean "LeftSingleClick". You should check out the Snoop utility for investigating exactly which events are getting triggered.
I found actually 2 ways to handle mouse events on controls with the mvvm pattern.
Both ways are actually 1 way:
MVVM Light Toolkit by http://mvvmlight.codeplex.com/
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand
Command="{Binding SelectionChangedCommand}"
CommandParameter="{Binding SelectedItems,
ElementName=MyDataGrid}" />
</i:EventTrigger>
</i:Interaction.Triggers>
and the Blend interactivity.dll with Behaviours
<i:Interaction.Triggers>
<i:EventTrigger EventName=”MouseLeftButtonDown”>
<Behaviours:ExecuteCommandAction Command=”{Binding MyCommand}” CommandParameter=”{Binding MyCommandParameter}”/>
</i:EventTrigger>
</i:Interaction.Triggers>
Do you know of any better method?
Moderator: Why the heck are my last 6 xaml lines of code not visible at all?
They are swallowed by IE and Iron browser.
Would you please report the admin to fix that code script? its not working at all very often. prove: http://img251.imageshack.us/img251/5236/errorxt.png
Those are both good ways to do it if you need to handle MouseDown in arbitrary places.
However these situations generally are few and far between. Usually there is a simpler way:
Are you sure your objects aren't really buttons that just don't look like buttons? If so, make them real Button objects and template them to look the way you want.
Are you sure your objects are just selection areas for objects in a list? If so, change the container from ItemsControl to ListBox and restyle ListBoxItem to use the selection areas.
Are your objects graphical paths that are being selected? Use a ToggleButton whose content is the path itself.
There are many other examples of this. In fact, it is uncommon to find a situation in which a MouseDown maps to a Command and there isn't a cleaner way to do the same thing.
There is always another option. You can handle WPF events in the code-behind of the View and call the appropriate method on the ViewModel. The MVVM pattern doesn't forbid to write any code in the code-behind file of the View.
The ViewModel sample application of the WPF Application Framework (WAF) shows how this can work.
XCommand Open source codeplex project has better way to deal with this event based Command/CommandParameter binding. Find here, xcommand.codeplex.com
Here is the sample code below:
<Grid>
<TextBlock Margin="20,30,20,0" VerticalAlignment="Top" Height="80" x:Name="XTextBlock"
Foreground="{Binding FgColor, Mode=TwoWay}"
XCmd:MouseMove.Command="{Binding TextBlockPointerMovedCommand}"
XCmd:MouseLeftButtonDown.Command="{Binding TextBlockPointerPressedCommand}"
XCmd:MouseLeave.Command="{Binding TextBlockPointerExitedCommand}"
Text="{Binding Description, Mode=TwoWay}">
</TextBlock>
<Grid Grid.Column="1" Background="{Binding BgColor, Mode=TwoWay}"
XCmd:MouseMove.Command="{Binding GridPointerMovedCommand}"
XCmd:MouseMove.CommandParameter="{Binding ElementName=XTextBlock, Path=Text}"
XCmd:MouseLeftButtonDown.Command="{Binding GridPointerPressedCommand}"
XCmd:MouseLeftButtonDown.CommandParameter="{Binding ElementName=XTextBlock, Path=Text}"
>
</Grid>
</Grid>
Hope this will be helpful.