views:

33

answers:

2

I have a parent contentcontrol which displays data via a datatemplate. The datatemplate contains a stackpanel with several usercontols of the same type. I like to set the property only once on the parent control, it must set the value of the property on all the subcontrols. But if there is a way to do it on the stackpanel it's also OK. The template can be changed at runtime and the values need also to be propagated to the new template.

My current solution is to implement the property on both parent and subcontrol and use code to propagate the value from the parent to all the subcontrols. My question is: is there a better or other ways of doing this?

EDIT: Some notes of clarification to my question. The application is currently WPF, but if it's portable to silverlight it would be a bonus. The property is a dependency of the type Style.

I want to use it to style part of the subcontrol. Currently the datatemplate is stored in a separate resource dictionary, so it can be reused. The visuals of the subcontrol are styled via controltemplate. The template contains three different controls, the first one is a label. The need (desire, foolish wish) is to set the style only once, to give the label on all the subcontrols in the datatemplate a consistent look and feel. So the crux of the problem is to override the value of the a style dependency property on a subcontrol, stored in a resource dictionary from a container control. Both are custom user controls, so all options are open.

<Parent SubSubStyle="x" Template="template" />

<DataTemplate x:Key=template>
  <StackPanel>
    <Subcontrol SubSubStyle="?"/>
    <Subcontrol SubSubStyle="?"/>
    <Subcontrol SubSubStyle="?"/>
    <Subcontrol SubSubStyle="?"/>
  </StackPanel>
</DataTemplate>
A: 

Style is best option http://msdn.microsoft.com/en-us/library/ms745683.aspx#styling_basics

Andrey
+1  A: 

Is the property that you're trying to set a DependencyProperty that you have created? If so, the ideal thing to do in WPF is to define the property such that it will be inherited by elements in the visual tree.

If it's not your own dependency property (or if you're using Silverlight which does not support this mechanism) then you should instead use implicit styles.

public class MyControl {

    // be prepared for some dependency property hell below
    // this defines a DependencyProperty whose value will be inherited
    // by child elements in the visual tree that do not override
    // the value. An example of such a property is the FontFamily
    // property. You can set it on a parent element and it will be
    // inherited by child elements that do not override it.

    public static readonly DependencyProperty MyInheritedProperty =
        DependencyProperty.Register(
            "MyInherited",
            typeof(string),
            typeof(MyControl),
            new FrameworkPropertyMetadata(
                null,
                FrameworkPropertyMetadataOptions.Inherits
            )
        );

}
Josh Einstein
It works, but I needed to use RegisterAttached(), not register to get the inheritance to work properly. The Child control needs AddOwner() + the inerits option.Thanks to all for the answers!
Andre van Heerwaarde
Hm, maybe I do recall running into that before. I remember working around it by creating an attached property on an unrelated class then doing AddOwner on both the parent control and child control. That way I didn't have to use attached property syntax when working with it.
Josh Einstein