リスト要素がエッジにある場合にそれらを異なるスタイルにするために、依存関係プロパティPosition
を定義します。添付プロパティリストの最初/最後の項目のビュースタイルに値が渡されません
は、私が(そのデフォルトと一緒に取得し、示されていないメソッドを設定)依存関係プロパティを持っている:
public static readonly DependencyProperty PositionProperty =
DependencyProperty.RegisterAttached(
"Position",
typeof(Position),
typeof(ClientView),
new FrameworkPropertyMetadata(
Position.Normal));
のTabControl:
<TabControl x:Name="Items" ItemContainerStyle="{DynamicResource TabItem}"/>
およびそれらのTabItemsのテンプレート:
<Style x:Key="TabItem" TargetType="{x:Type TabItem}">
... <Setter Property="Template"> <Setter.Value>
<Grid SnapsToDevicePixels="true">
<ControlTemplate TargetType="{x:Type TabItem}">
<Border x:Name="Bd">
<StackPanel Orientation="Horizontal">
<TextBlock
Text="{Binding Position, RelativeSource={RelativeSource
AncestorType={x:Type client:ClientView}}}"/>
<ContentPresenter x:Name="Content" ContentSource="Header"/>
</StackPanel>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="client:ClientView.Position" Value="Last">
<Setter Property="CornerRadius" TargetName="Bd" Value="0,0,0,4"/>
</Trigger>
...
私のClientView
クラスのコードビハインドでは、t彼のアイテム・ジェネレータは、彼がそれらのtabitemsを作成したことを私に伝えます:
var gen = Items.ItemContainerGenerator;
gen.StatusChanged += (sender, args) =>
{
if (gen.Status == GeneratorStatus.ContainersGenerated)
{
var cnt = Items.Items.Count;
if (cnt > 0)
{
if (cnt == 1)
{
gen.ContainerFromItem(Items.Items[0])
.SetValue(PositionProperty, Position.Normal);
}
else
{
gen.ContainerFromItem(Items.Items[0])
.SetValue(PositionProperty, Position.First);
if (cnt > 2)
{
for (int i = 1; i < cnt - 2; i++)
{
gen.ContainerFromItem(Items.Items[i])
.SetValue(PositionProperty, Position.Normal);
}
}
gen.ContainerFromItem(Items.Items[cnt - 1])
.SetValue(PositionProperty, Position.Last);
}
}
}
};
私はこのコードを実行すると、私はそれらのイベントをデバッグし、正しい値が設定されている参照が、それらはビューに表示されることはありません。テキストボックスのテキストは、常にデフォルト値である "Normal"を表示します。 ここで何が間違っていますか?あなたが添付プロパティがスタイルのTabItemに設定されているので、この
<TextBlock Text="{Binding
Path=(ClientView.Position),
RelativeSource={RelativeSource Mode=TemplatedParent}}" />
ようTextBlock.Text
プロパティをバインドバインドする場合、どのよう
ではなく、トリガ<のControlTemplateで、あなたのスタイルにDataTriggerを試してみてください。トリガー>
SvenG