Reputation: 247
I am trying to get my program to copy a textblock from a grid to the Clipboard after it has successfully done it once. I have a SelectionChanged event that copies the data to the clipboard I than go to an editor paste the clipboard and it works fine. I than copy some other data to the clipboard. I than switch back to the program and double click the mouse to copy the data to the clip board again. I would like to just use the MouseDown
or MouseLeftButtonClick
event but I can't seem to get them to work. Do only some Mouse events work in this scenario?
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1">
<StackPanel Orientation="Horizontal" ScrollViewer.CanContentScroll="True" Width="auto" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
</Grid>
<TextBlock Width="130" Grid.Column="0" ScrollViewer.CanContentScroll="True" x:Name="TextSelected" Text="{Binding Description}" TextWrapping="Wrap" Margin="5,0,10,0" HorizontalAlignment="Stretch" />
<TextBlock Width="150" Grid.Column="1" ScrollViewer.CanContentScroll="True" x:Name="TextCommand" Text="{Binding Command}" Margin="0,0,10,0" TextWrapping="Wrap" HorizontalAlignment="Stretch" />
<TextBlock Width="200" Grid.Column="2" ScrollViewer.CanContentScroll="True" x:Name="TextLocation" Text="{Binding Location}" Margin="0,0,10,0" TextWrapping="Wrap" HorizontalAlignment="Stretch" />
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding AddItemBtn}" CommandParameter="{Binding ElementName=AddItemList2,Path=SelectedItem}" />
</i:EventTrigger>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding AddItemBtn}" CommandParameter="{Binding ElementName=AddItemList2,Path=SelectedItem}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
Upvotes: 2
Views: 1225
Reputation: 6552
You should capture PreviewMouseDown
or PreviewMouseLeftButtonDown
instead.
For example:
<i:EventTrigger EventName="PreviewMouseDown">
<i:InvokeCommandAction Command="{Binding PreviewMouseDownCommand}" CommandParameter="{Binding ElementName=AddItemList2,Path=SelectedItem}" />
</i:EventTrigger>
The reason that MouseDown
and MouseLeftButtonDown
does not work as you expected is because those are bubbling events that are raised by the ListBoxItem
which are already handled by the ListBoxItem
itself.
In applications, it is quite common to just handle a bubbling routed event on the object that raised it
PreviewMouseDown
and PreviewMouseLeftButtonDown
are tunneling events which are invoked on your root element first then travels through the child elements. Usually, we bind
the root element (maybe a UserControl
or Window
) to the DataContext
thus giving you the opportunity to handle the event there.
You can read more about Tunneling/Bubbling events here (quoted statement above also taken here): https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/routed-events-overview
Upvotes: 2