WPF, setting a property with a single value on multiple subcontrols

I have a parent content control that displays data via a datatemplate. The dataset contains a stacking panel with several custom contours of the same type. I like to set the property only once in the parent control, it has to set the property value in all subcontrollers. But if there is a way to do it in the stack pane, that is also OK. The template can be changed at runtime and the values ​​must also be passed to the new template.

My current solution is to implement the property on both the parent and subcontroller and use code to propagate the value from the parent to all subcontrollers. My question is, are there any better or other ways to do this?

EDIT: Some comments to clarify my question. It is currently a WPF application, but if it is ported to Silverlight it would be a bonus. The property is a dependency of the Style type.

I want to use it to style the subcontrol part. The data file is currently stored in a separate resource dictionary so it can be reused. Visual effects of subcontrol are formatted using a checklist. The template contains three different controls, the first is the label. The need (wish, stupid wish) is to set the style just once, to give the shortcut on all subcontrollers in the datatemplate a consistent look and feel. Therefore, the main problem is to override the value of the style dependency property from subcontrol stored in the resource dictionary from the 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>

      

+2


a source to share


2 answers


Is the property you are trying to set the DependencyProperty that you created? If so, then in WPF the ideal task is to define a property that will be inherited by elements in the visual tree.

If this is not your own dependency property (or if you are using Silverlight, which does not support this mechanism), you should use implicit styles instead .



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
            )
        );

}

      

+1


a source


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



0


a source







All Articles