【问题标题】:Binding inside the ItemsPanelTemplate from Parent in Windows phone从 Windows 手机中的父项绑定到 ItemsPanelTemplate
【发布时间】:2012-07-03 08:54:47
【问题描述】:

我想更改 ItemsPanelTemplate 内的 StackPanel 的方向...要更改方向,我已经实现了一个属性.. 有什么方法可以更改 StackPanel 的方向。我尝试了以下代码..但它没有成功。

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

【问题讨论】:

    标签: c# xaml windows-phone-7.1 itemspaneltemplate


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      RelativeSource.TemplatedParent 用于ControlTemplates

      参考一些示例here

      【讨论】:

      • 我想更改 ItemsPanelTemplate 内部的 StackPanel 的方向。但是您给出的帖子对我没有帮助...抱歉..
      • 示例提供一些想法来使用它,而不是完全用于您的查询..所以我告诉过,参考示例。
      猜你喜欢
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 2015-06-27
      • 2013-10-29
      相关资源
      最近更新 更多