3

ItemsPanelTemplate 内にある StackPanel の方向を変更したい...方向を変更するには、1 つのプロパティを実装しました..StackPanel の方向を変更する方法はありますか。次のコードを試しました..しかし、成功しません。

<Setter Property="ItemsPanel">
    <Setter.Value>
        <ItemsPanelTemplate>
             <StackPanel Orientation="{Binding MyOrientation,RelativeSource={RelativeSource TemplatedParent}}" HorizontalAlignment="Left" />
        </ItemsPanelTemplate>
    </Setter.Value>
</Setter>       
4

2 に答える 2

3

使用する{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>

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

于 2014-10-04T21:02:13.253 に答える
1

RelativeSource.TemplatedParentは。用ControlTemplatesです。

ここでいくつかのサンプルを参照してください

于 2012-07-04T04:39:53.900 に答える