Syntax for Nested Attached Properties

Published 10-08-2008 9:07 AM | JohnBowen

Attached Properties are usually expressed using attribute syntax and take advantage of type conversion. This works well for things that use basic types like Grid.Row, DockPanel.Dock, ScrollViewer.HorizontalScrollBarVisibility, etc. Sometimes you may need to use nested element syntax to set a value that can't be expressed as a convertible string. This comes into play most often with custom attached properties. For a built-in attached property the syntax looks a little weird but is pretty simple:

<Window x:Class="Namespace.Window1" ...>
    <Grid.Row>1</Grid.Row>
</Window>

There's not really any reason for setting Grid.Row like this but it helps to demonstrate how to get to the syntax for custom properties.

<Window x:Class="Namespace.Window1" xmlns:local="clr-namespace:Namespace" ...>
    <local:HelperUtility.SomeData>
        <local:DataObject  />
    </local:HelperUtility.SomeData>
</Window>

Here we've set a custom attached property to an instance of a custom type. What if you need a collection of objects? There are a few options depending on how your attached property is set up. Using the ArrayExtension:

<Window x:Class="Namespace.Window1" xmlns:local="clr-namespace:Namespace" ...>
    <local:HelperUtility.SomeData>
        <x:ArrayExtension Type="{x:Type local:DataObject}">
            <local:DataObject />
            <local:DataObject />
        </x:ArrayExtension>
    </local:HelperUtility.SomeData>
</Window>

or adding items to a collection:

<Window x:Class="Namespace.Window1" xmlns:local="clr-namespace:Namespace" ...>
    <local:HelperUtility.DataList>
        <local:DataObject />
        <local:DataObject />
        <local:DataObject />
    </local:HelperUtility.DataList>
<Window.Resources>

Filed under:

Comments

# 2008 October 09 - Links for today « My (almost) Daily Links said on October 8, 2008 11:39 PM:

Pingback from  2008 October 09 - Links for today « My (almost) Daily Links