Следующий Style
покажет AutoscrollListBox
как содержимое DataTemplate
. После выбора ListBoxItem
вместо него будет отображаться MarqueeTextBlock
. Если он не выбран, он переключится обратно на AutoscrollListBox
.
Решение 1
<Grid>
<AutoscrollListBox Name="Items" >
<AutoscrollListBox.ItemTemplate>
<DataTemplate>
<DataTemplate.Resources>
<AutoscaleTextBlock x:Key="AutoscaleTextBlock"
Text="{Binding Title}"
Style="{StaticResource ListBoxStyle_BASETB}"
x:Shared="False" />
<MarqueeTextBlock x:Key="MarqueeTextBlock"
Text="{Binding Title}"
Style="{StaticResource AOTbMain_MARQUEE}"
x:Shared="False" />
</DataTemplate.Resources>
<ContentControl x:Name="ContentPresenter" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}"
Value="False">
<Setter TargetName="ContentPresenter"
Property="Content"
Value="{StaticResource AutoscaleTextBlock}"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}"
Value="True">
<Setter TargetName="ContentPresenter"
Property="Content"
Value="{StaticResource MarqueeTextBlock}"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</AutoscrollListBox.ItemTemplate>
</AutoscrollListBox>
</Grid>
Решение 2
В случае, если выработать со словарями вложенных ресурсов и не может применить атрибут x:Shared
, вы можете поменять элементы управления непосредственно из Trigger
:
<Grid>
<AutoscrollListBox Name="Items" >
<AutoscrollListBox.ItemTemplate>
<DataTemplate>
<ContentControl x:Name="ContentPresenter" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}"
Value="False">
<Setter TargetName="ContentPresenter"
Property="Content">
<Setter.Value>
<AutoscaleTextBlock Text="{Binding Title}"
Style="{StaticResource AOTbMain_MARQUEE}" />
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}"
Value="True">
<Setter TargetName="ContentPresenter"
Property="Content">
<Setter.Value>
<MarqueeTextBlock Text="{Binding Title}"
Style="{StaticResource AOTbMain_MARQUEE}"/>
</Setter.Value>
</Setter>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</AutoscrollListBox.ItemTemplate>
</AutoscrollListBox>
</Grid>
Solution 3
или переключитьVisibility
:
<Grid>
<AutoscrollListBox Name="Items" >
<AutoscrollListBox.ItemTemplate>
<DataTemplate>
<Grid>
<AutoscaleTextBlock x:Name="AutoscaleTextBlock"
Text="{Binding Title}"
Style="{StaticResource AOTbMain_MARQUEE}"
Visibilty="Visible" />
<MarqueeTextBlock x:Name="MarqueeTextBlock"
Text="{Binding Title}"
Style="{StaticResource AOTbMain_MARQUEE}"
Visibilty="Collapsed" />
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}"
Value="True">
<Setter TargetName="AutoscaleTextBlock"
Property="Visibility"
Value="Collapsed" />
<Setter TargetName="MarqueeTextBlock"
Property="Visibility"
Value="Visible" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</AutoscrollListBox.ItemTemplate>
</AutoscrollListBox>
</Grid>