【问题标题】:Unfocused ListBox, item style未聚焦的列表框,项目样式
【发布时间】:2013-07-08 10:57:27
【问题描述】:

好的,我不敢相信没有关于此的在线资源。我想做一件简单的事情,并在 ListBox 项目被选中并且其父 ListBox 失去焦点时更改其样式。

我们一直在为此使用VisualStateManager,但由于SelectedSelectedFocusedFocused 状态存在重叠,因此在选择持有 ctrl 的项目时引入了一些错误(例如,错误的项目出现选择)。我决定使用Triggers 修复它,发现当ListBox 失去焦点时似乎无法触发。

我的问题是实现此行为的正确方法是什么,请不要说“覆盖 SystemColors”...

编辑:

好的,我对这两个答案都投了赞成票,但选择了 Viv 的回答,因为他的回答使它与原始 ListBox 完全一样,而我在鼠标悬停和我已经使用的其他样式方面没有问题。我已经看到Selector 附加属性的用法,但从未尝试过IsSelectionActive,它就像一个魅力。尽管VisualStateManager 在 WPF 中较新,但我建议您使用此类问题的触发器。我认为显然存在一些可以避免重叠状态的问题。

再次感谢 Viv 和 Richard 为我的问题提供了 2 种实施解决方案的绝佳示例。

【问题讨论】:

    标签: c# wpf listbox styles focus


    【解决方案1】:

    这里是Style 用于使用VisualStateManagerListBoxItem。它以我期望的方式突出显示项目。

      <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>
    

    【讨论】:

    • 是的,这仅使用 SelectionStates...问题是我的 FocusStates 与 SelectionStates 重叠。我想我可能没有它们,我会试一试,如果它工作正常,请告诉你。
    【解决方案2】:

    好的,我不敢相信没有关于此的在线资源。我想做一件简单的事情,并在 ListBox 项目被选中并且它的父 ListBox 失去焦点时更改其样式。

    猜你在寻找MultiTriggerIsSelected=trueSelector.IsSelectionActive=false

    类似:

    <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>
    

    现在,当左侧ListBox 中的一个项目被选中时,如果实际的ListBox 失去焦点,它将得到一个红色Foreground,例如:

    Foreground 只是一个示例,使用MultiTrigger 您可以根据需要调整Style

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多