简体   繁体   中英

Dependency Inheritance for Resources

I've got a somewhat special problem: I have a markup extension that needs to check an attached property. The attached property is defined as inheritable.

This works properly in XAML for all elements that are defined under a Window/UserControl, but not for resources. For example, given this XAML:

<Window MyAttachedProp="...">
    <Window.Resources>
        <FlowDocument x:Key="Doc">
            <Paragraph><Run Text="{MyMarkupExtension}" /></Paragraph>
        </FlowDocument>
    </Window.Resources>

    <FlowDocumentScrollViewer Document="{StaticResource Doc}" />
    ...
</Window>

The markup extension fails because when the ProvideValue()-function is called, the attached property is not derived from the Window to the FlowDocument and to the Run.

Is there same workaround to make this work?

Thanks, Steven

From Inheriting Property Values Across Tree Boundaries :

Property inheritance works by traversing a tree of elements. This tree is often parallel to the logical tree

Apparently a resource dictionary does not constitute an element tree and hence the value won't be inherited. However, i guess it should work when you set the property on the FlowDocument, since that is the root of an element tree:

<Window.Resources>
    <FlowDocument x:Key="Doc" MyAttachedProp="...">
        <Paragraph><Run Text="{MyMarkupExtension}" /></Paragraph>
    </FlowDocument>
</Window.Resources>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM