zoukankan      html  css  js  c++  java
  • WPF 中 通过点击ListBox中的元素自动选中一整项

    由于WPF中,经常有个问题点击Listbox中的里面的元素经常不会选中某项,选中的项也不会变颜色,这个其实可以通过修改样式去实现。

            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="SnapsToDevicePixels"
                        Value="True" />
                <Setter Property="FocusVisualStyle"
                        Value="{x:Null}" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <Border Name="Border"
                                    Padding="1 1 1 1"
                                    Background="Transparent"
                                    SnapsToDevicePixels="True"
                                    CornerRadius="20">
                                <ContentPresenter />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected"
                                         Value="True">
                                    <Setter TargetName="Border"
                                            Property="Background"
                                            Value="#D84E20" />
                                    <Setter Property="Foreground"
                                            Value="White" />
                                </Trigger>
                                <Trigger Property="IsEnabled"
                                         Value="false">
                                    <Setter Property="Foreground"
                                            Value="White" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsKeyboardFocusWithin"
                             Value="true">
                        <Setter Property="IsSelected"
                                Value="true" />
                    </Trigger>
    
                    <EventTrigger RoutedEvent="PreviewGotKeyboardFocus">
                        <BeginStoryboard>
                            <Storyboard>
                                <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(ListBoxItem.IsSelected)">
                                    <DiscreteBooleanKeyFrame KeyTime="0"
                                                             Value="True" />
                                </BooleanAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Style.Triggers>
            </Style>

    留个笔记作为参考,方便以后使用。感谢您的浏览。

    多看书,少装逼!
  • 相关阅读:
    Dijkstra-leetcode 743.网络延迟时间
    BFS-leetcode787 K站中转内最便宜的航班
    图论基础——单源最短路径问题
    DFS leetcode-547 朋友圈
    SpringBoot 使用注解向容器中注册Bean的方法总结
    SpringBoot对SpringMVC的支持
    数据源简介
    Spring MVC简介
    2020-2-10 Python 列表切片陷阱:引用、复制与深复制
    2020-2-2 语法糖
  • 原文地址:https://www.cnblogs.com/R00R/p/14687906.html
Copyright © 2011-2022 走看看