我想更改ItemsPanelTemplate内部的StackPanel的方向.要更改Orientation,我已经实现了一个属性.是否有任何方法可以更改StackPanel的方向.我尝试了以下代码..但是没有成功.Setter Property=ItemsPanelSetter.Valu...
我想更改ItemsPanelTemplate内部的StackPanel的方向.要更改Orientation,我已经实现了一个属性.是否有任何方法可以更改StackPanel的方向.我尝试了以下代码..但是没有成功.
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="{Binding MyOrientation,RelativeSource={RelativeSource TemplatedParent}}" HorizontalAlignment="Left" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
解决方法:
使用{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
沃梦达教程
本文标题为:c#-在Windows Phone中从父项绑定到ItemsPanelTemplate中
猜你喜欢
- C#委托与冒泡排序实例 2023-05-31
- Unity实现10天签到系统 2023-04-14
- C#实现简易多人聊天室 2023-05-17
- 聊聊C# 中HashTable与Dictionary的区别说明 2023-03-29
- c# 单例模式的实现方法 2023-03-09
- c# – 加速LINQ to SQL查询 2023-11-15
- C#使用winform实现进度条效果 2023-06-27
- C#中把Json数据转为DataTable 2023-06-04
- Unity3D Shader实现流光效果 2023-02-16
- C#中数据类型的转换介绍 2023-05-12