Unfocused ListBox, item style

扶醉桌前 提交于 2019-12-06 13:57:35

OK, I can't believe there is no online resources about this. I want to do a simple thing and change style of a ListBox item when it's selected and it's parent ListBox has lost focus.

guess your looking for a MultiTrigger with IsSelected=true and Selector.IsSelectionActive=false?

so something like:

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>
  <ListBox Margin="15">
    <ListBox.ItemContainerStyle>
      <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="Foreground"
                Value="Blue" />
        <Style.Triggers>
          <MultiTrigger>
            <MultiTrigger.Conditions>
              <Condition Property="IsSelected"
                          Value="true" />
              <Condition Property="Selector.IsSelectionActive"
                          Value="false" />
            </MultiTrigger.Conditions>
            <Setter Property="Foreground"
                    Value="Red" />
          </MultiTrigger>
        </Style.Triggers>
      </Style>
    </ListBox.ItemContainerStyle>
    <ListBoxItem Content="A" />
    <ListBoxItem Content="B" />
    <ListBoxItem Content="C" />
  </ListBox>
  <ListBox Grid.Column="1"
            Margin="15">
    <ListBoxItem Content="A" />
    <ListBoxItem Content="B" />
    <ListBoxItem Content="C" />
  </ListBox>
</Grid>

Now when an item in the left ListBox is selected and then if the actual ListBox looses focus, it would get a Red Foreground like:

Foreground is just an example, using the MultiTrigger you can tweak the Style as you see fit.

Here is a Style for the ListBoxItem that used the VisualStateManager. It highlights the items in the manner that I would expect.

  <Style TargetType="ListBoxItem">
        <Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="Border"
                            Background="Transparent"
                            CornerRadius="3"
                            BorderThickness="1"
                            Padding="2">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected"/>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                        Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                            <EasingColorKeyFrame KeyTime="0"
                                                                 Value="DodgerBlue"/>
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="SelectedUnfocused">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                        Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                            <EasingColorKeyFrame KeyTime="0"
                                                                 Value="CornflowerBlue"/>
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <ContentPresenter/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!