Is there an easy way to make anything draggable in Silverlight XAML? - silverlight

My target is an entire listbox clipped in a canvas, but I guess there should be something that can work for all controls?
How to make a control draggable and even attached x or y constraints?

You would have to cycle thru all the child controls and add handlers to mouse events.
Within those events you control the drag and the bounds.

Related

WPF - adorner stays in original position after view transitions

I have a control template defined, call it myVal, that is used for validation - this is then used for example in a Style targeting textbox where its Validation.ErrorTemplate is set as
Now say there are a number of such textboxes that sit in a view and that this slides in using TranslateTransform and BeginAnimation.
The result is that the adorner used in the ErrorTemplate doesn't follow the position of the textboxes as the view transitions - instead these stay in the starting position. However, the adorners reposition themselves correctly in relation to the textboxes as soon as I set focus or events such as mouse move.
How can I get the adorners to show in the correct position after the transformation without having to change the focus? Is there a way of delaying the validation until after the transition...or how can I "revalidate" the properties once the animation has finished? I read somewhere about calling invalidatevisual but can't see how I'd do that. Any help is much appreciated.
Cheers
Two ideas:
Try adding an AdornerDecorator around the textbox, or around the group of textboxes. This will tell WPF to add another layer for rending adorners. Adding a layer "closer" to the textboxes might help.
If you want to tell the adorner layer to re-render itself, then you can use something like the following code:
var al = AdornerLayer.GetAdornerLayer(myTextBox);
al.Update();

MouseWheel: Scrolling vs. Zooming

I've got a Silverlight 4 custom control that basically is several Canvas elements wrapped inside a ScrollViewer. The user can set a property to determine whether to scroll or zoom when using their mouses wheel. In the custom control's MouseWheel event, I check to see if they want to scroll or zoom. If zooming, I determine the delta and modify the custom control's zoom level (which then handles the zooming code for me).
The problem is that zooming won't start until the ScrollViewer's current position of the vertical scrollbar is at the top or bottom of the scrollbar. Once their, then the zooming works perfectly.
Does anyone have any suggestions on how I can prevent scrolling completely so that I only zoom (when the user wants to zoom, that is)?
Thanks!
Looks like one of my child elements was hogging the MouseWheel event. I traced this by adding Debug.WriteLine statements to each of the child element's MouseWheel event as well as the parent control's MouseWheel event.
So, I can't blame SL4. Just myself. :)

Resizing controls on a canvas

I have a Silverlight (v3) application that users can drag controls (e.g. Shapes, Images) onto, change the fill colour, drag around etc
I am looking for the best method to allow the user to click on a control, give a visible indication that the control can be reiszed (e.g. display resize handles) and then handle the resizing.
I have played with a few ways of doing this but I am not sure of the best way to make this as clean/generic as possible - ideally I would like to mark a control as resizable and then have common code implement this. I have tried 1) adding a ControlTemplate to a control and 2) handling the MouseLeftButtonDown event and adding a new rectangle which surrounds the object and then resizing the original control as this rectangle is resized.
Does anyone have experience of implementing this, some good code resources?
Have a look at this Resize Behavior

How can I pass a mouse click to a parent control?

In a Silverlight 2 app, I am using Rectangles on a Canvas to draw a representation of data. I would like to have mouse clicks on the Rectangles be passed on to the control that owns the Canvas. I would also like to be able to show a tooltip with a summary of the data when the mouse is over a Rectangle.
So far I've only been able to achieve one item or the other. If I make the Rectangles have their IsHitTestVisible property = false, the tooltips don't work, but the owning control will receive the mouse clicks. If I set it to true, then tooltips do work, but the clicks don't get passed on.
Is there a way to have a Silverlight item be IsHitTestvisible = true, and pass on the mouse clicks?
I'm not sure why the tooltip and mouse left down / up is being linked, In SL3 the mouse left down / up will bubble unless it gets marked as handled by a routine / object. It sounds more like the event is not bubbling thru the parent controls. AFAIK SL2 does the same. (could be wrong on that one)
What is the visual tree from the outer parent to the inner rectangle?
Have Canvas pass a reference of itself to Rectangles when they're constructed. When a rectangle is clicked, call a method on Canvas.
Edit:
If Rectangles and Canvas are library classes, subclass them to add functionality you need.

Binding a Popup to another control's relative screen position

I'm writing an XBAP with a complex Popup (Canvas Z-index of 99 with a grid on it...) that I would like to "attach" to the button that opens it and follow that button around wherever it goes on the screen. For example, if the button is in a ListBox or an XamDataGrid I would like the popup to follow the button as it scrolls through. If it is beneath an Expander I want it to stay attached to the button when the expander forces it to move, etc.
Any Ideas?
When using a Popup, neither PlacementTarget nor CustomPopupPlacementCallback is used after the popup has originally appeared. So any use of these properties will not allow the popup to track the button as it moves.
Several ways occur to me of achieving what you desire:
Attach a custom Adorner to the button and put the popup inside it. Disadvantage: Popup is not connected to Button or surrounding elements, so it won't inherit properties & DataContext from them.
Attach a custom Adorner to the button. This adorner will get measure and arrange calls when the button moves relative to the AdornerLayer, allowing you to manually update the Popup position. As long as your AdornerDecorator doesn't move relative to your Window (eg if it is the direct child of the Window), you can easily detect the AdornerLayer being moved by monitoring changes to Window size. Disadvantage: Complex to code & get right.
Don't use a Popup at all. Instead wrap the button in a <Grid> alongside a <Canvas> with zero width and height and the desired position. Inside the <Canvas> add the UserControl for the popup with an appropriate ZIndex. It will extend past the edge f the Canvas, which is just fine in WPF. Instead of using a Popup control just control the visibility of the UserControl. Disadvantage: Will not really be totally on top of all other objects, can't extend off edge of window (may not be an issue for XBAP, though).
I'm not sure if it will auto-update for you or not, but the PlacementTarget property allows you to specify a control to position the popup relative to. If that doesn't work, then maybe CustomPopupPlacementCallback will do the trick?

Resources