2012-07-03 89 views
3

我想更改ItemsPanelTemplate內部的StackPanel的方向...要更改方向,我已經實現了一個屬性..有什麼方法可以改變StackPanel的方向。我嘗試了下面的代碼..但它沒有成功。從Windows手機的父項ItemsPanelTemplate內綁定

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

回答

1

RelativeSource.TemplatedParent用於ControlTemplates

參考一些樣品here

+1

我想改變的StackPanel的取向,這是ItemsPanelTemplate..But裏面,你給不幫助過我的帖子...抱歉.. –

+0

樣品給一些想法使用這個,不完全是爲了你的查詢..所以我告訴,參考樣本。 – Ponmalar

2

使用{的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

相關問題