Reputation: 54183
I have Rectangles that use a Thumb to be able to be dragged around. They can be dragged by clicking and dragging anywhere on the rectangle.
Right now, I want to be able to add a rectangle centered where the user moused down on the parent canvas. When I do, I want the rectangle to receive the mousedown and be able to be dragged around.
However, the rectangle receives nothing until I mouse up and mouse down again.
I tried the previewMouseDown etc, I tried e.Handled = true, etc, but none of them work. The event is always consumed by the parent canvas.
Is there a mechanism even above preview that can be like: Well right now you are set to click on the parent canvas, then I add the rectangle, oh but now you are set to click on the rectangle so ultimately I will give the mousedown to the rectangle.
Upvotes: 0
Views: 162
Reputation: 76
i haven't seen your code, therefore i can't say it for sure.. assume that you use interactivity eventtrigger from mvvmlight for mousedown.. what you can do is that implement your mousedown command inside the rectangle class, then you should be able to drag your rectangle around... Assume if you have a canvas class (canvas.cs) in your c#, in your canvas.cs, you have an object call Rect (Rectangle Rect = new Rectangle(...)), you can implement your mousedowncommand inside your rectangle.cs instead of canvas.cs. There are lots of way of doing this.. or inside your xaml of mainwindow.. you can do the binding that reference to the Rect class.. For example, in your mainwindow.xaml.cs, you set the datacontext to Canvas.cs.. in your mainwindow.xaml, you can bind the mousedown like this:
<Window
xmlns:i="clr namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF45">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown or PreviewMouseDown">
<cmd:EventToCommand Command="{Binding Canvas.Rect.MouseDownCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Window>
Alternatively, you can also do
<Your Control Here>
<Border>
<Border.InputBinding>
<MouseBinding MouseAction="LeftClick" Command="{Binding DataContext.SomeCommand}"/>
</Border.InputBindings>
</Border>
</Your Control Here>
Upvotes: 1