GetAdornerLayer() for a control derived from MedialElementBase - wpf

I have a WPF control which is derived from MedialElementBase(Base class is UIElement).
This control is used to dislay video from webcam. I need to draw lines on this video.
I am using WPF Adorners and calling GetAdornerLayer() for above control which always
returns NULL. Am I using this correctly? Any idea why this is happening?

An AdornerLayer is created by an AdornerDecorator or a ScrollContentPresenter in the visual tree. By default the ControlTemplate for Window has one surrounding all its content. If there is neither an AdornerDecorator or ScrollContentPresenter as a parent to your control, it will return null.

Related

Is there always an Adorner Layer between a parent content control and its children?

Just trying to clarify an observation. (I can't seem to find a straight answer from Google). I am doing some image editing using adorners. My images are both in an ItemsControl and as children of an InkCanvas.
When using GetAdornerLayer() for an element within the ItemsControl, I automatically obtain an Adorner Layer over the element within the ItemsTemplate. But if I go further up the visual tree with VisualTreeHelper, I find another AdornerLayer above all the items of the ItemsControl. (The ItemsControl is itself a child of a Grid). On the other hand, when accessing the AdornerLayer for a child of the InkCanvas, I get an adorner layer that lies between the InkCanvas itself and its children.
Hence, in both cases, it seems apparent that WPF is always placing an adornerlayer between a contentcontrol and its children.
Is this indeed the case?
TIA.
It depends on how the ControlTemplates of the controls are defined. The AdornerLayer.GetAdornerLayer method traverses up the visual tree starting at the specified Visual and returns the adorner layer of the first AdornerDecorator or ScrollContentPresenter element it finds.
So WPF doesn't automatically "always placing an adornerlayer between a contentcontrol and its children". It is rather the control author that provides the adorner layer by adding an AdornerDecorator to the visual tree.
If you for example look at the default ControlTemplate for the Window, you will see that it indeed includes an <AdornerDecorator> element.

why did the arrows in my treeview turn to strange squares

I have an user control which has a treeview. This user control inherits from a base user control.
I was able to see the arrows in the treeview well till I changed the base control graphics.
From that point I started to see strange squares instead of the regular arrow in the treeview.
Is there any reason for this behavior?
This behavior was influenced by a style that was defined in the app.xaml.
The target type of the style was Border.
Probably the type of the arrows in the treeview is some border, and the public border type in the app xaml caused those arrows to be changed.

UserControl Storyboard objects moving out of UserControl

I have a UserControl that have a storyboard that moves a control (within my UserControl) out of the usercontrol (using a TranslateX RenderTransform).
When I move the object out of the control it shows on the parent Page (that hosts my UserControl). Is there a way to just hide it when it reaches the boundaries of my UserControl?
At the end of your storyboard set the animated controls visibility to collapsed/opacity = 0. If that animation is too abrupt for you, animate the opacity to 0 as the storyboard progresses. To detect when the animation goes out of your control would be rather difficult. You're probably best off "guessing" by waiting a few tenths of a second.
You could try setting the Canvas.ZIndex so that the Control is behind the Parent Control
The Z-Index doesn't work since its outside my UserControl.
I was thinking more like using the "Clip" property, but I'm not really familiar with it :/
What I finally did :
Use a Canvas (instead of a Grid) as my LayoutRoot of my UserControl
Added a Canvas.Clip that matches the size of my UserControl
On the SizeChanged of my UserControl I resize my Clip to fit the new size.
I'd like to post the XAML here but somehow the CodeSample does not work :/ Sorry

Silverlight - Fill a Rectangle (or other controls which have a Brush Property) with a custom UserControl

I need to fill a Rectangle with a custom UserControl. The rectangle's .Fill property accepts a Brush and in Silverlight there is no equivalent for VisualBrush.
I've found this post - http://chriscavanagh.wordpress.com/2009/09/24/silverlight-visualbrush-and-rounded-corners/ - with a possible solution. However this approach requires the UserControl (which will be used to fill the rectangle) to be rendered first outside the rectangle so that the VisualImage can convert it to a WritableBitmapImage.
Does anyone know any alternate solution? I would prefer not having to render the user control outside the rectangle and remove it afterwards, because there is the possibility for some flickrs to occur.
Thanks and best regards,
Bruno
There is no alternate solution if you must be using a rectangle and brush for its fill.
I take it you already have a reason not to simply use a Border containing the UserControl directly? If you don't want the usercontrol to response to the mouse you could include in the Border a Grid containing both your UserControl and a Rectangle with a transparent fill.

Forcing a repaint of a WPF control

I currently try to create classes for a paint-like WPF application. I have to base classes LineMovement (line from StartPoint to EndPoint) and PathMovement (line going through all points specified in a property Points of type PointCollection). These classes inherit from Control and get their looks through a ControlTemplate.
The ControlTemplate also adds an Adorner to the AdornerLayer of the Movement objects containing a little visual marker for every moveable point of the specific line. These markers support dragging with a mouse.
The problem I have is that somehow my Movement classes don't repaint when their points are moved. I debugged my code with Mole and found out that the Polyline used to visualize the line gets the changed point values (visible in its Points property) but it just doesn't repaint.
How can I force a repaint of a WPF control?
TemplateBinding doesn't support two-way data binding (i.e. updating the Points collection with the new values of the Polyline). It's meant only for one-way data binding for use in control templates. See Bea Stollnitz's blog entry: http://bea.stollnitz.com/blog/?p=38
Turns out that TemplateBinding is pure evil.
When I bind the Points of the Polyline by {TemplateBinding Points} it doesn't update itself, whereas when I bind it with {Binding RelativeSource={RelativeSource TemplatedParent}} it works perfectly.
Note to myself: Never use this goddamn TemplateBinding again.
You need to make your Movement objects' DPs have the AffectsArrange metadata property (http://msdn.microsoft.com/en-us/library/system.windows.frameworkpropertymetadataoptions.aspx) - that way when the property changes, WPF knows it should redraw

Resources