zoukankan      html  css  js  c++  java
  • wp7,listBox选中项颜色改变

    您可以使用 ItemContainerGenerator 并基于项的索引来检索项或通过指定数据项来检索容器。例如,如果您有一个绑定了数据的ItemsControl,并希望基于其索引获取一个项,则可以使用 ItemContainerGenerator..::..ContainerFromIndex 方法。如果想检索数据项,可使用 ItemContainerGenerator..::..ItemFromContainer 方法。

    1)代码:

    listBox的选中项的背景色是实时的,因此在页面中定义一个实时变量 private int _index = -1;

    因此在listBox的SelectionChanged事件中,index=-1表示没有选中项,直接选中项赋背景色;index有具体的值,之前的选中项_index恢复到原来的背景色null:

     if (_index != -1)
         (listData.ItemContainerGenerator.ContainerFromIndex(_index) as ListBoxItem).Background = null;
     (listData.ItemContainerGenerator.ContainerFromItem(listData.SelectedItem) as ListBoxItem).Background = new SolidColorBrush(Colors.Red);
     _index = listData.SelectedIndex;//获取此次选中项的index

    补充:

    a.Background是背景色,Foreground字体颜色,根据需要选择;

    b.很多情况下并不是颜色并不是null,有些时候可能是英文Red|Blue等,这样很简单,直接Colors.Red就好,有些时候可能是十六进制#265546,或者RGB这个时候就麻烦点,我们要进行转换,幸运的是系统已经提供了方法:

    把十六进制颜色转化为color对象
    ColorTranslator.FromHtml("#FF0000")
    或 ColorTranslator.FromHtml("Red");

    把color对象转化为十六进制颜色
    ColorTranslator.ToHtml(Color.FromArgb(255,255,255))
    或 ColorTranslator.ToHtml(Color.Red);

    等等,这个ColorTranslator类在System.Drawing下,具体的可以详见Drawing类;

    2)style:

    意:写样式要写itemcontainer的样式!!

     
    1>字体颜色设置,选中

    然后选中states,在selected和unselected的时候分别设置Forground的颜色,
     

     
    2>背景色颜色设置:
    首先把layoutroot改成grid或者border或是canvas等。

    然后和字体颜色改变的方法一样,分别在selected和unselected的时候分别设置Background的颜色。
    最后在xaml中引用该样式就可以~
    附上代码:
     
     
    <Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="BorderBrush" Value="Transparent"/>
                <Setter Property="Padding" Value="0"/>
                <Setter Property="HorizontalContentAlignment" Value="Left"/>
                <Setter Property="VerticalContentAlignment" Value="Top"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="Normal"/>
                                        <VisualState x:Name="MouseOver"/>
                                        <VisualState x:Name="Disabled">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TransparentBrush}"/>
                                                </ObjectAnimationUsingKeyFrames>
                                                <DoubleAnimation Duration="0" To=".5" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ContentContainer"/>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                    <VisualStateGroup x:Name="SelectionStates">
                                        <VisualState x:Name="Unselected">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="White"></DiscreteObjectKeyFrame>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="Selected">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="Blue"/>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

    主要就是Unselected和Selected状态下写Storyboard动画板,这样就可以了,原文楼主地址:http://www.cnblogs.com/asnowTT/archive/2012/01/11/2319714.html,稍微改进了一点,感谢

  • 相关阅读:
    UML统一建模语言笔记
    从零开始学JavaWeb
    也谈微信小程序
    Memcached,你懂的
    一个简单的配置管理器(SettingManager)
    我的AngularJS 学习之旅
    .NET Core 跨平台
    ASP.NET Core 中间件自定义全局异常处理
    面试必考题——递归解题套路
    程序员着装指南
  • 原文地址:https://www.cnblogs.com/Yukang1989/p/2745939.html
Copyright © 2011-2022 走看看