WPF Trigger for IsSelected in a DataTemplate for ListBox items
<DataTemplate DataType="{x:Type vm:HeaderSlugViewModel}">
<vw:HeaderSlugView />
DataTemplate>
<DataTemplate DataType="{x:Type vm:ContentSlugViewModel}">
<vw:ContentSlugView />
DataTemplate>
<DataTemplate DataType="{x:Type vm:ImageSlugViewModel}">
<vw:ImageSlugView />
DataTemplate>
Each "View" is an independent XAML file. I'd like to be able to set up the triggers in those files, looking at the ListBoxItem's IsSelected property, in order to control the visibility of the various controls within.
The template to override the ListBoxItem follows:
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border Name="SlugContainer" Background="Transparent" BorderBrush="Black" BorderThickness="1" CornerRadius="2" Margin="0,5,0,0" Padding="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
Grid.ColumnDefinitions>
<ContentPresenter Grid.Row="1" />
Grid>
Border>
ControlTemplate>
Setter.Value>
Setter>
Style>
I modified the ContentPresenter in the following way in order to test out using the "FindAncestor" RelativeSource:
<ContentPresenter Grid.Row="1">
<ContentPresenter.Style>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="False">
<Setter Property="Visibility" Value="Collapsed"/>
DataTrigger>
Style.Triggers>
Style>
ContentPresenter.Style>
ContentPresenter>
This works, but when I move similar code into the XAML file representing a View it no longer sees the trigger. For example:
<UserControl ...>
<UserControl.Resources>
<local:FlowDocumentToXamlConverter x:Key="flowDocumentConverter" />
UserControl.Resources>
<UserControl.Style>
<Style TargetType="{x:Type UserControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}">
<Setter Property="Visibility" Value="Hidden" />
DataTrigger>
Style.Triggers>
Style>
UserControl.Style>
<DockPanel Name="SlugContainer">
<Label DockPanel.Dock="Top" Content="Filler" />
<ctrl:BindableRichTextBox x:Name="TextBox" Document="{Binding Content, Converter={StaticResource flowDocumentConverter}, Mode=TwoWay}" LostFocus="OnLostFocus" />
DockPanel>
UserControl>
How can I detect, preferably from the View's XAML file, when the ListBoxItem has been selected?
I notice that DataTrigger.Value property is missing in UserContrl's Style. With the following changes, I think your code would work.
<Style TargetType="{x:Type UserControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
<Setter Property="Visibility" Value="Hidden" />
DataTrigger>
Style.Triggers>
Style>
다음으로 전송:https://www.cnblogs.com/smiler/p/3317581.html
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.