Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TemplateBinding in ItemsPanelTemplate

I'm building a custom ItemsControl in Silverlight that (amongst other things) allows items to be displayed horizontally or vertically at runtime. How can I bind the Orientation property of the ItemsPanel to the Orientation property of my parent control? I've tried using TemplateBinding (which works inside the ControlTemplate) but does not seem to work inside the ItemsPanelTemplate, am I doing something wrong?

<Style TargetType="CustomItemsControl">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel Orientation="{TemplateBinding Orientation}" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>
like image 292
Paul Bevis Avatar asked Dec 05 '25 05:12

Paul Bevis


1 Answers

Use a RelativeSource:

<Style TargetType="CustomItemsControl">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel Orientation="{Binding Orientation, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CustomItemsControl}}}" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>

Edit after comment: Silverlight doesn't support RelativeSource, but this post by Colin Eberhardt explains how it can be implemented manually.

like image 63
Kent Boogaart Avatar answered Dec 08 '25 00:12

Kent Boogaart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!