不专心的ListBox,项目样式 [英] Unfocused ListBox, item style

查看:67
本文介绍了不专心的ListBox,项目样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我无法相信没有关于此的在线资源.我想做一个简单的事情,并在选择ListBox项目的父项ListBox失去焦点时更改其样式.

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.

我们一直使用VisualStateManager来实现此目的,但是由于SelectedSelectedFocusedFocused状态存在重叠,因此在选择包含ctrl的项目时会引入一些错误(错误的项目显示为被选择) ).我决定使用Trigger修复它,发现ListBox失去焦点时似乎无法触发.

We have been using VisualStateManager for this purpose, but since there is overlapping in Selected, SelectedFocused and Focused states, there were some bugs introduced when selecting items holding ctrl for example(wrong items appear selected). I decided to fix it using Triggers and found out that there seems to be no way of triggering when ListBox lost focus.

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

My question is what is the right way of implementing this behavior, and please don't say "override SystemColors"...

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

OK, I have up-voted both answers but picked Viv's response because his answer makes it work exactly like original ListBox while I don't have problems with mouse-over and other styles I already use. I have already seen usage of Selector attached property, but never tried IsSelectionActive, it worked like a charm. I would recommend going for triggers for this type of problem, although the VisualStateManager is newer in WPF. I think there are clearly some problems with overlapping states that can be avoided.

再次感谢Viv和Richard提供了两种解决我的问题的方法的出色示例.<​​/p>

Thanks again to Viv and Richard for providing great examples of 2 ways of implementing the solution for my problem.

推荐答案

好的,我无法相信没有关于此的在线资源.我想做一个简单的事情,并在选择ListBox项并且其父级ListBox失去焦点时更改样式.

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.

猜测您要使用IsSelected=trueSelector.IsSelectionActive=falseMultiTrigger吗?

类似这样:

<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只是一个示例,使用MultiTrigger您可以根据需要调整Style.

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

这篇关于不专心的ListBox,项目样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆