UWP实现ListBox颜色相间显示
我们经常会在各种软件中见到两种颜色相间的ListBox,在UWP下如何创建,先看效果图
要实现的效果有ListBoxItem颜色相间显示,选中的项呈蓝色,鼠标经过的项呈黄色
1 <Page 2 x:Class="ID3App.SecondPage" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:local="using:ID3App" 6 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 7 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 8 xmlns:interop="using:Windows.UI.Xaml.Interop" 9 mc:Ignorable="d"> 10 <Page.Resources> 11 </Page.Resources> 12 <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 13 <Grid.RowDefinitions> 14 <RowDefinition Height="100"></RowDefinition> 15 <RowDefinition Height="*"/> 16 </Grid.RowDefinitions> 17 <TextBlock Text="This is the secondPage" FontSize="40" HorizontalAlignment="Center" Grid.Row="0"></TextBlock> 18 <ListBox Grid.Row="1" ItemsSource="{Binding SongList}" SelectionChanged="ListBox_SelectionChanged" > 19 <ListBox.ItemContainerStyle> 20 <Style TargetType="ListBoxItem"> 21 <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" /> 22 <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" /> 23 <Setter Property="Background" Value="Transparent"/> 24 <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" /> 25 <Setter Property="TabNavigation" Value="Local"/> 26 <Setter Property="IsHoldingEnabled" Value="True"/> 27 <Setter Property="Padding" Value="12,0,12,0"/> 28 <Setter Property="HorizontalContentAlignment" Value="Left"/> 29 <Setter Property="VerticalContentAlignment" Value="Center"/> 30 <Setter Property="MinWidth" Value="{ThemeResource ListViewItemMinWidth}"/> 31 <Setter Property="MinHeight" Value="{ThemeResource ListViewItemMinHeight}"/> 32 <Setter Property="UseSystemFocusVisuals" Value="False" /> 33 <Setter Property="Template"> 34 <Setter.Value> 35 <ControlTemplate TargetType="ListBoxItem"> 36 <Grid x:Name="ContentBorder" Background="Transparent" BorderBrush="Transparent" BorderThickness="0"> 37 <VisualStateManager.VisualStateGroups> 38 <VisualStateGroup x:Name="CustomStates"> 39 <VisualState x:Name="Unselected"> 40 </VisualState> 41 <VisualState x:Name="CustomSelected"> 42 <Storyboard> 43 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextGrid" Storyboard.TargetProperty="Background"> 44 <DiscreteObjectKeyFrame KeyTime="0" Value="Yellow" /> 45 </ObjectAnimationUsingKeyFrames> 46 </Storyboard> 47 </VisualState> 48 </VisualStateGroup> 49 <VisualStateGroup x:Name="CommonStates"> 50 <!--一般情况下的样式--> 51 <VisualState x:Name="Normal"> 52 <Storyboard> 53 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextGrid" Storyboard.TargetProperty="Background"> 54 <DiscreteObjectKeyFrame KeyTime="0" Value="{Binding Color}" /> 55 </ObjectAnimationUsingKeyFrames> 56 </Storyboard> 57 </VisualState> 58 <!--鼠标经过的项--> 59 <VisualState x:Name="PointerOver"> 60 <Storyboard> 61 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextGrid" Storyboard.TargetProperty="Background"> 62 <DiscreteObjectKeyFrame KeyTime="0" Value="Yellow" /> 63 </ObjectAnimationUsingKeyFrames> 64 </Storyboard> 65 </VisualState> 66 <!--按下某项的瞬间--> 67 <VisualState x:Name="Pressed"> 68 <Storyboard> 69 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground"> 70 <DiscreteObjectKeyFrame KeyTime="0" Value="Red" /> 71 </ObjectAnimationUsingKeyFrames> 72 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter1" Storyboard.TargetProperty="Foreground"> 73 <DiscreteObjectKeyFrame KeyTime="0" Value="Yellow" /> 74 </ObjectAnimationUsingKeyFrames> 75 </Storyboard> 76 </VisualState> 77 <!--无法触发--> 78 <VisualState x:Name="Selected"> 79 <Storyboard> 80 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TextGrid" Storyboard.TargetProperty="Background"> 81 <DiscreteObjectKeyFrame KeyTime="0" Value="Blue" /> 82 </ObjectAnimationUsingKeyFrames> 83 </Storyboard> 84 </VisualState> 85 <!--鼠标进过选中的项--> 86 <VisualState x:Name="SelectedPointerOver"> 87 </VisualState> 88 <VisualState x:Name="SelectedUnfocused"> 89 <!--<Storyboard> 90 91 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground"> 92 <DiscreteObjectKeyFrame KeyTime="0" Value="Red" /> 93 </ObjectAnimationUsingKeyFrames> 94 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter1" Storyboard.TargetProperty="Foreground"> 95 <DiscreteObjectKeyFrame KeyTime="0" Value="Yellow" /> 96 </ObjectAnimationUsingKeyFrames> 97 98 </Storyboard>--> 99 </VisualState> 100 <!--点击选中的项--> 101 <VisualState x:Name="SelectedPressed"> 102 </VisualState> 103 </VisualStateGroup> 104 <VisualStateGroup x:Name="DisabledStates"> 105 <VisualState x:Name="Enabled"/> 106 <VisualState x:Name="Disabled"> 107 </VisualState> 108 </VisualStateGroup> 109 </VisualStateManager.VisualStateGroups> 110 <!--<Rectangle x:Name="BorderBackground" IsHitTestVisible="False" 111 Fill="{ThemeResource SystemControlHighlightListAccentLowBrush}" 112 Opacity="0" Control.IsTemplateFocusTarget="True"/--> 113 <Grid Background="Blue" x:Name="TextGrid"> 114 <Grid.ColumnDefinitions> 115 <ColumnDefinition Width="30"></ColumnDefinition> 116 <ColumnDefinition Width="*"></ColumnDefinition> 117 </Grid.ColumnDefinitions> 118 <TextBlock x:Name="ContentPresenter" Text="{Binding Num}"/> 119 <TextBlock Grid.Column="1" x:Name="ContentPresenter1" Text="{Binding Song}"/> 120 </Grid> 121 122 </Grid> 123 </ControlTemplate> 124 </Setter.Value> 125 </Setter> 126 </Style> 127 </ListBox.ItemContainerStyle> 128 </ListBox> 129 </Grid> 130 </Page>
由于Selected的样式无法触发,使得选中的项不能变为蓝色,我采用了另一种思路,将Normal的样式设为相间颜色,而将ListBoxItem的原始颜色设为蓝色,以实现相同的效果,数据绑定的代码如下:
private List<ListStyle> _songList;public List<ListStyle> SongList {get { return _songList; }set{_songList = value;OnPropertyChanged();} }
public class ListStyle {public int Num { get; set; }public string Song { get; set; }public SolidColorBrush Color { get; set; } }
如果您还有更好的实现方式,非常欢迎于我交流,谢谢
转载于:https://www.cnblogs.com/JohnHwangBlog/p/6096402.html
UWP实现ListBox颜色相间显示相关推荐
- 用计算机画出方格表,方格造型图_怎么做这种颜色相间的方格图(有图)_彩妆阁...
1.怎么做这种颜色相间的方格图(有图) 经典做法: 1.新建4*4像素的画布,背景色为白. 2.将画布放大到最大,在画布四个角各填充一个像素的黑色块(可以用选框工具选中一个像素),画布中间填充一个四个 ...
- WPF ListBox颜色交替及以击事件实现
ListBox 中的数据行实现颜色交替 WPF内置的两大专用属性ItemsControl.AlternationCount 属性和 ItemsControl.AlternationIndex 附加属性 ...
- php对表格的处理,JavaScript_js处理表格对table进行修饰,js处理表格 1、行颜色间隔显示 - phpStudy...
js处理表格对table进行修饰 js处理表格 1.行颜色间隔显示 css样式:两个选择器 .one{ background-color:#33ffcc; } .two{ backgound-colo ...
- css①字体颜色正常显示,背景透明②字体颜色与背景均为透明的设置方法
①字体颜色正常显示,背景颜色透明 color:#fff; background:rgba(0,0,0,0.5); ②字体颜色与背景颜色均为透明 color:#fff; background:#00 ...
- 用 ListBox 和 DataBinding 显示列表数据 (木野狐译)
作者: Scott Guthrie 出处: http://blog.joycode.com/scottgu/ [原文地址]Silverlight Tutorial Part 5: Using th ...
- Silverlight 教程第五部分:用 ListBox 和 DataBinding 显示列表数据 (木野狐译)
[原文地址]Silverlight Tutorial Part 5: Using the ListBox and DataBinding to Display List Data [原文发表日期] F ...
- 用 ListBox 和 DataBinding 显示列表数据 (木野狐译) 1
作者: Scott Guthrie 出处: [url]http://blog.joycode.com/scottgu/[/url] [原文地址]Silverlight Tutorial Part ...
- Silverlight 中文教程第五部分:用 ListBox 和 DataBinding 显示列表数据 (木野狐译)
这是8个系列教程的第5部分,这个系列示范如何使用 Silverlight 2 的 Beta1 版本来创建一个简单的 Digg 客户端应用.这些教程请依次阅读,将有助于您理解 Silverlight 的 ...
- Silverlight教程第五部分:用 ListBox 和 DataBinding 显示列表数据 (木野狐译)
这是8个系列教程的第5部分,这个系列示范如何使用 Silverlight 2 的 Beta1 版本来创建一个简单的 Digg 客户端应用.这些教程请依次阅读,将有助于您理解 Silverlight 的 ...
最新文章
- linux carry php Soap 扩展
- 深入理解Struts2中的OGNL表达式
- EventBus源码解析
- linux游戏脚本,ubuntu 新手一键配置脚本
- android 之Dialog对话框(简易版)
- DVWA文件上传high级文件上传漏洞
- Java集合系列---ConcurrentHashMap源码解析
- 【白皮书分享】2022年新品营销白皮书-阿里妈妈.pdf(附下载链接)
- 【Kalman】卡尔曼滤波器工作原理(Link)
- aspnetpager分页UI调用存储过程函数
- storage theory
- java某个参数值设置为空_@PathVariable为空时指定默认值的操作
- java所参数查询_Java程序查询系统参数
- Java集合11 (Queue)
- 比较万能的匹配邮箱的正则表达式
- 匿名mahony互补滤波代码详解
- 计算软件介绍siesta、vasp、wien2k、PWSCF、Materials Studio
- 小学五年级计算机课评课,小学生信息技术课《复制与变换》评课稿
- pytorch转onnx: step = 1 is currently not supported以及Exporting the operator silu to ONNX opset version
- 计算日期间隔,以XX年XX月XX日格式显示