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>

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

    多看书,少装逼!
  • 相关阅读:
    SQL_server 将表中的记录 转换成 Insert(插入) SQL 语句
    Delphi DBGridEh导出Excel
    hdu 2018 母牛的故事
    hdu 2084 数塔
    hdu 2190 重建希望小学
    hdu 2501 Tiling_easy version
    hdu 2046 骨牌铺方格
    hdu 2045 不容易系列之(3)—— LELE的RPG难题
    高精度模板
    各种平面分割问题总结(转)
  • 原文地址:https://www.cnblogs.com/R00R/p/14687906.html
Copyright © 2011-2022 走看看