1
votes

I have the following style for a ListBoxItem defined:

    <Style x:Key="detailListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="AutomationProperties.AutomationId" Value="{Binding Path=StringTableKey}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="Padding" Value="2,0,0,0"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border x:Name="Bd" Margin="4" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="Background" TargetName="Bd" Value="{DynamicResource MenuItemSelectedBackgroundBrush}"/>
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                    </Trigger>
                    <Trigger Property="IsSelected" Value="false">
                        <Setter Property="Background" TargetName="Bd" Value="{DynamicResource MenuItemUnSelectedBackgroundBrush}"/>
                    </Trigger>
                    <!-- This is the case when a detail is selected (the master list loses focus). -->
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="true"/>
                            <Condition Property="Selector.IsSelectionActive" Value="false"/>
                        </MultiTrigger.Conditions>
                        <!--<Setter Property="Opacity" TargetName="Bd" Value=".4"/>-->
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The ListBox for which this style is applied is defined as:

<ListBox 
    x:Name="listBox"
    Margin="0,60,0,0"
    MaxHeight="600"
    Foreground="Transparent"
    Style="{StaticResource detailListBoxStyle}" 
    ItemContainerStyle="{StaticResource detailListBoxItemStyle}" 
    ItemsSource="{Binding Source={StaticResource detailCollectionViewSource}}" 
    ItemTemplateSelector="{StaticResource detailDataTemplateSelector}"
    TouchDown="ListBoxTouchDown"
    TouchMove="ListBoxTouchMove"
    TouchUp="ListBoxTouchUp"
    KeyDown="ListBoxKeyDown">
    <ListBox.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Margin="0,10,0,0" FontWeight="Bold" FontSize="20" Foreground="White" Text="{Binding Path=Name}"/>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ListBox.GroupStyle>
</ListBox>

I have a DataTemplate for a ListBoxItem that can be:

<DataTemplate x:Key="detailOnOffTemplate">
    <Grid Height="50" Width="{StaticResource detailWidth}">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock x:Name="tb1" Margin="4,2,0,0" Grid.Row="0" Style="{StaticResource MenuTextStyle}" Text="{Binding DisplayName}" VerticalAlignment="Top" TextAlignment="Left">
         <TextBlock.Effect>
            <DropShadowEffect Color="White" ShadowDepth="0" BlurRadius="7"/>
        </TextBlock.Effect>
        </TextBlock>
    </Grid>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}" Value="True">
            <Setter TargetName="tb1" Property="Foreground" Value="White"/>
            <Setter TargetName="tb1" Property="Effect" Value="{x:Null}"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

I need to be able to bind to "Selector.IsSelectionActive" from within my DataTemplate but nothing works. I've tried these things:

            <DataTrigger Binding="{Binding Selector.IsSelectionActive, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}" Value="True">

            <Trigger Binding="{Binding Selector.IsSelectionActive, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}" Value="True">

            <Trigger "Selector.IsSelectionActive" Value="True">

Basically, I want the same trigger that is contained in the ControlTemplate in my DataTemplate:

                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="true"/>
                            <Condition Property="Selector.IsSelectionActive" Value="false"/>
                        </MultiTrigger.Conditions>
                        <!--<Setter Property="Opacity" TargetName="Bd" Value=".4"/>-->
                    </MultiTrigger>

Or, how else can I know the item "IsSelected" but does not have keyboard focus?

2

2 Answers

7
votes

The first option you tried would be correct exception you're not identifying that that is an attached property so it looks like you are trying to bind to a property named Selector that returns an object that has a property names IsSelectionActive. So I would think something like the following would work:

<DataTrigger Binding="{Binding Path=(Selector.IsSelectionActive), RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Value="True">
0
votes

The previous answer didn't work for me, but after many hours of playing this did:

The key here (I think) was that the IsSelectionActive FALSE comes first. And that trigger is also a multitrigger combined with the IsSelected. I didn't have to use brackets in this case. But that might be because I'm using the latest .net framework, Wpf and Visual Studio, so you may need the brackets spoken of in the previous answer.

The red, yellow and white is just for this sample, remember to change those colors.

<ControlTemplate.Triggers>
    <MultiTrigger>
        <!-- selected, but not focused -->
        <MultiTrigger.Conditions>
            <Condition Property="Selector.IsSelectionActive" Value="False" />
            <Condition Property="IsSelected" Value="True" />
        </MultiTrigger.Conditions>
        <Setter Property="Background" TargetName="Bd" Value="Yellow" />
    </MultiTrigger>
    <MultiTrigger>
        <!-- selected, and focused -->
        <MultiTrigger.Conditions>
            <Condition Property="Selector.IsSelectionActive" Value="True" />
            <Condition Property="IsSelected" Value="True" />
        </MultiTrigger.Conditions>
        <Setter Property="Background" TargetName="Bd" Value="Red" />
        <Setter Property="Foreground" Value="White" />
    </MultiTrigger>                    
</ControlTemplate.Triggers>