WPF trigger for IsSelected in a DataTemplate for ListBox items

I have a list and I have the following ItemTemplate for it:

<DataTemplate x:Key="ScenarioItemTemplate"> <Border Margin="5,0,5,0" Background="#FF3C3B3B" BorderBrush="#FF797878" BorderThickness="2" CornerRadius="5"> <DockPanel> <DockPanel DockPanel.Dock="Top" Margin="0,2,0,0"> <Button HorizontalAlignment="Left" DockPanel.Dock="Left" FontWeight="Heavy" Foreground="White" /> <Label Content="{Binding Path=Name}" DockPanel.Dock="Left" FontWeight="Heavy" Foreground="white" /> <Label HorizontalAlignment="Right" Background="#FF3C3B3B" Content="X" DockPanel.Dock="Left" FontWeight="Heavy" Foreground="White" /> </DockPanel> <ContentControl Name="designerContent" Visibility="Collapsed" MinHeight="100" Margin="2,0,2,2" Content="{Binding Path=DesignerInstance}" Background="#FF999898"> </ContentControl> </DockPanel> </Border> </DataTemplate> 

As you can see, ContentControl is set to Visibility for Reset.

I need to define a trigger that causes Visibility to be set to "Visible"

when ListItem is selected, but I can not understand.

Any ideas?

UPDATE: Of course, I could just duplicate the DataTemplate and add triggers to the ListBox in question, use one or the other, but I want to prevent duplication of this code.

+51
triggers wpf listbox itemtemplate event-triggers
Oct 29 '08 at 21:44
source share
2 answers

You can create your ContentControl so that the trigger starts when its container (ListBoxItem) is selected:

 <ContentControl x:Name="designerContent" MinHeight="100" Margin="2,0,2,2" Content="{Binding Path=DesignerInstance}" Background="#FF999898"> <ContentControl.Style> <Style TargetType="{x:Type ContentControl}"> <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> </Style.Triggers> </Style> </ContentControl.Style> </ContentControl> 

Alternatively, I think you can add a trigger to the template itself and reference the control by name. I don’t know this technique well enough to type it from memory and assume that it will work, but it’s something like this:

 <DataTemplate x:Key="ScenarioItemTemplate"> <DataTemplate.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True"> <Setter TargetName="designerContent" Property="Visibility" Value="Visible"/> </DataTrigger> </DataTemplate.Triggers> ... </DataTemplate> 
+97
Oct 29 '08 at 21:58
source share

@Matt, thanks !!!

It was just necessary to add a trigger for IsSelected == false, and now it works like a charm!

 <ContentControl.Style> <Style TargetType="{x:Type ContentControl}"> <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> 

+3
Oct 29 '08 at 22:14
source share



All Articles