代码之家  ›  专栏  ›  技术社区  ›  Vijay

当焦点移出WPF列表框时,所选项目将失去样式

  •  26
  • Vijay  · 技术社区  · 17 年前

    ListBox 用XML文件中的项目填充。给定a DynamicResource Style 属性和书面触发器 IsSelected ItemContainerStyle .

    .

    我面临什么问题?

    触发器工作。但是,当我将焦点移出列表框(按tab键或单击其他控件)时,所选项目将失去其样式。是否有任何方法可以保留所选项目样式?

    提前感谢!

    2 回复  |  直到 15 年前
        1
  •  1
  •   Community Mohan Dere    9 年前

    The answer referenced

    <ListBox....>
        <ListBox.Resources>
                <Style TargetType="ListBoxItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBoxItem">
                                <Border Name="Border" Padding="2" SnapsToDevicePixels="true">
                                    <ContentPresenter />
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="true">
                                        <Setter TargetName="Border" Property="Background"
                                                Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
            </Style>
        </ListBox.Resources>
    </ListBox>
    

    这将做的是,每当选择列表框项时(无论控件状态如何),都会在列表框项上设置突出显示背景颜色。

    http://blogs.vbcity.com/xtab/archive/2009/06/29/9344.aspx

        2
  •  30
  •   Thies    16 年前

    如果您只设置背景颜色,请尝试替换 ControlBrush this answer .

    推荐文章