ItemsControl.AlternationIndex
我试图附加一个属性,就像ItemsControl
. 像下面这样使用它会导致构建时出现“找不到模板属性”错误。附加的属性AlternationIndex
工作正常。
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid SnapsToDevicePixels="true">
<Border x:Name="Bd" ... >
<ContentPresenter x:Name="Content" ... />
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="ItemsControl.Position"
Value="Last">
<Setter Property="CornerRadius"
TargetName="Bd"
Value="0,0,0,4"/>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex"
Value="0">
<Setter Property="CornerRadius"
TargetName="Bd"
Value="4,0,0,0"/>
</Trigger>
...
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
该属性的代码放置在包含模板设置器的同一 WPF 控件的代码隐藏中。
public enum Position
{
First,
Normal,
Last
}
private static readonly DependencyPropertyKey PositionPropertyKey =
DependencyProperty.RegisterAttachedReadOnly(
"Position",
typeof(Position),
typeof(ItemsControl),
new FrameworkPropertyMetadata(Position.Normal,
FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty PositionProperty =
PositionPropertyKey.DependencyProperty;
public static Position GetPosition(DependencyObject element)
{
if (element == null)
throw new ArgumentNullException("element");
var result = element.GetValue(PositionProperty);
return (Position)result;
}
internal static void SetPosition(DependencyObject d, Position value)
{
d.SetValue(PositionPropertyKey, value);
}
internal static void ClearPosition(DependencyObject d)
{
d.ClearValue(PositionPropertyKey);
}
需要什么才能使该属性在 XAML 中可见和可用?