Basically I have created an "Adorner" class for Silverlight which allows the user to Move, Scale and Rotate any object it adorns. The way this works is the Adorner watches for any mouse events on it's child element and responds accordingly.
This works great for simple elements like ellipses and rectangles. But I have a custom element which contains an image and a label inside a Canvas.
My problem is this. I want the user to be able to move the label independantly from the image, but maintain this movement as an offset only (Hence why I am not just letting them add an image and a label seperately). This means that the label will need to make use of the Canvas.Left and Canvas.Top properties. However, whenever I add these, the label becomes unclickable... i.e. The element no longer recognizes the click anymore when the label is clicked, however it does still respond to the user clicking the image.
I assume this has something to do with the way the Canvas element has no automatic layout controlling.
The alternative is to use a Grid and set Margin.Top and Margin.Left which works, but it doesn't allow a negative margin - therefore the label will never be able to appear above and to the left of the image which is not acceptable for our needs. (This is not strictly true, it CAN appear above and to the left, but it stops responding to mouse events when it has a negative margin)
Therefore I was wondering if there was anyway I could catch the mouse click event on the label (MouseLeftButtonDown) and then somehow pretend it came directly from the c开发者_开发百科anvas - I believe this would solve my issue but I don't know if it is possible.
If anyone has any ideas they would be much appreciated!!
Edit: Here is the XAML for my custom element. The element does NOT fire the MouseLeftButtonDown event when the TextBlock is clicked but DOES when the Image is clicked.
<UserControl x:Class="SitePlanViewer.Components.Visual.BoreholeElement"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Border x:Name="Border" BorderBrush="Transparent" BorderThickness="2" Margin="-5">
<Canvas x:Name="MainCanvas" Margin="5" MinWidth="12" MinHeight="12">
<Image x:Name="BoreholeImage" Source="/Images/BoreholeSymbol.png" VerticalAlignment="Center" Width="12" Height="12" />
<TextBlock x:Name="Label" FontSize="8" Canvas.Left="40" Canvas.Top="20" />
</Canvas>
</Border>
</UserControl>
You can't automate Mouse events to elements as that would open a security flaw (remember you can only invoke certain operations from actual mouse clicks, like file open dialogs, so they do not allow simulating the clicks).
Your example works for the image only because the canvas is physically under the image.
Mouse events are propagated to a canvas only if the canvas extent is directly under the mouse (e.g. if you make the canvas even smaller the image clicks will stop propagating to the canvas as well).
If you increase the size of the canvas (e.g. to include all possible positions of the label) it will get the events, but if you have multiple BoreholeElements on a single page, you will have overlapping canvas objects (which is obviously bad).
You may have to rework your approach to the problem. Perhaps one large canvas with all the child objects on it?
Could you construct the relevant EventArgs
object and then call the OnClick
method directly pass the EventArgs
精彩评论