Anton__

Hello

I have a custom control and an according style defined in generic.xaml
I'm instantiating the control on the adorner layer and for some reason the style is not applied to the control.

The style is certainly applied if the control is instantiated on the application level.

What's the trick with the adorners

Thanks
Anton


Re: Windows Presentation Foundation (WPF) The reason why the Style from generic.xaml is not applied?

Yi-Lun Luo - MSFT

Hello, how can you add a Control to an Adorner When you create an Adorner, you override OnRender method. In this method, you can draw any geometries such as Rectangles and Ellipses. But you won¡¯t be able to add a Control to it. If you need to add Controls, consider using a Popup rather than an Adorner.






Re: Windows Presentation Foundation (WPF) The reason why the Style from generic.xaml is not applied?

Zhou Yong

I've sort of answered this question in another MSDN thread.

Although Josh's problem is with the DP inheritance, since resource/style lookup mechanism works approximately the same as DP inheritance does except for those classes implementing IFreezable (Nick Krammer has a brilliant blog post on this topic instead)

To put it simple, in order to let the UIElements hosted inside the adorner layer to pick up the default styles, those UIElements shall be part of the visual tree or logical tree. Imagine you want to host a single UIElement in adorner layer, you can write a helper Adorner class as follows:

Code Block

public class UIElementAdorner : Adorner
{
private UIElement child;
public UIElementAdorner(UIElement element, UIElement adornedElement)
: base(adornedElement)
{
this.child = element;
base.AddLogicalChild(element);
base.AddVisualChild(element);
}

protected override Size MeasureOverride(Size constraint)
{
this.child.Measure(constraint);
return child.DesiredSize;
}

protected override Size ArrangeOverride(Size finalSize)
{
this.child.Arrange(new Rect(finalSize));
return finalSize;
}

protected override Int32 VisualChildrenCount
{
get { return 1; }
}

protected override Visual GetVisualChild(Int32 index)
{
return this.child;
}

protected override System.Collections.IEnumerator LogicalChildren
{
get
{
yield return this.child;
}
}
}

Note the call to base.AddLogicalChild(element) and base.AddVisualChild(element) and LogicalChildren override, these three steps can effectively add the interesting UIElement into the logical tree and visual tree respectively.

For how the resource lookup mechanism relates to the element tree concept in WPF, you can refer to:

http://msdn2.microsoft.com/en-us/library/ms753391.aspx

http://msdn2.microsoft.com/en-us/library/ms750613.aspx