Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding inside the ItemsPanelTemplate from Parent in Windows phone

I want to change the Orientation of StackPanel which is inside the ItemsPanelTemplate... To change the Orientation i have implemented one property.. Is there any way to change the orientation of StackPanel. I tried the following code.. But it is not succeeded.

<Setter Property="ItemsPanel">
    <Setter.Value>
        <ItemsPanelTemplate>
             <StackPanel Orientation="{Binding MyOrientation,RelativeSource={RelativeSource TemplatedParent}}" HorizontalAlignment="Left" />
        </ItemsPanelTemplate>
    </Setter.Value>
</Setter>       
like image 208
David Bekham Avatar asked Jan 23 '26 11:01

David Bekham


2 Answers

Use {RelativeSource AncestorType=YourItemsControlType}

MyItemsControl.cs:

namespace MyNamespace.Controls
{
    public partial class MyItemsControl : ItemsControl
    {
        public static readonly DependencyProperty MyOrientationProperty =
            DependencyProperty.Register(
            "MyOrientation",
            typeof(Orientation),
            typeof(MyItemsControl));

        public Orientation MyOrientation
        {
            get { return (Orientation)GetValue(MyOrientationProperty); }
            set { SetValue(MyOrientationProperty, value); }
        }
    }
}

MyItemsControls.xaml

<Style xmlns:source="clr-namespace:MyNamespace.Controls" TargetType="source:MyItemsControl">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel 
                    Orientation="{Binding Orientation, 
                        RelativeSource={
                            RelativeSource AncestorType=source:MyItemsControl
                        }
                    }"
                    HorizontalAlignment="Left" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>

Reference: http://msdn.microsoft.com/en-us/library/ms743599%28v=vs.110%29.aspx

like image 102
Marcos Avatar answered Jan 26 '26 00:01

Marcos


RelativeSource.TemplatedParent is for ControlTemplates.

Refer some samples here

like image 21
Ponmalar Avatar answered Jan 26 '26 01:01

Ponmalar



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!