当前位置:网站首页>WPF 修改 ItemContainerStyle 鼠标移动到未选中项效果和选中项背景
WPF 修改 ItemContainerStyle 鼠标移动到未选中项效果和选中项背景
2022-08-04 16:54:00 【林德熙】
本文告诉大家如何通过修改 ItemContainerStyle 让 ListView 或 ListBox 的选择效果如鼠标移动到未选中项的效果或选择项的背景
先写一些简单的代码用于界面的绑定
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
Items = new List<Item> { new Item(1), new Item(2), new Item(3) };
}
public List<Item> Items { get; set; }
}
public class Item
{
public Item(int id)
{
Id = id;
}
public int Id { get; set; }
public string Text { get => $"This is Item number {Id}"; }
}在界面放一个 ListView 默认在鼠标移动到没有被选择的项的时候会出现背景
<ListView ItemsSource="{Binding Items}">
<ListView.ItemTemplate>
<DataTemplate DataType="local:Item">
<StackPanel>
<TextBlock Text="{Binding Id}" />
<TextBlock Text="{Binding Text}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>通过写样式在 ItemContainerStyle 可以让 ListView 的在鼠标移动到未选择项的特效的颜色修改
<!-- set SelectedBackgroundColor to Transparent when you do not need the background in selected items -->
<Color x:Key="SelectedBackgroundColor">#00FFFFFF</Color>
<Color x:Key="SelectedUnfocusedColor">#FFB2A3A2</Color>
<!-- set the MouseOverColor to Transparent when you do not need the effect in the unselected items -->
<Color x:Key="MouseOverColor" >#00FFFFFF</Color>
<Style x:Key="ListViewItemStyle"
TargetType="ListViewItem">
<Setter Property="SnapsToDevicePixels"
Value="true" />
<Setter Property="OverridesDefaultStyle"
Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border x:Name="Border"
Padding="2"
SnapsToDevicePixels="true"
Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver" >
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Panel.Background).
(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0"
Value="{StaticResource MouseOverColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled" />
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected" />
<VisualState x:Name="Selected">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Panel.Background).
(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0"
Value="{StaticResource SelectedBackgroundColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="SelectedUnfocused">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Panel.Background).
(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0"
Value="{StaticResource SelectedUnfocusedColor}" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>可以直接使用上面的代码,如果想要让用户看不到鼠标移动到未选中项的特效可以设置 MouseOverColor 为透明,通过设置 SelectedBackgroundColor 可以让选中项的背景修改
因为颜色在 WPF 使用 #AARRBBGG 表示,如上面代码设置了 #00FFFFFF 就是透明,因为第一个 Alpha 为 0 也就是透明
在 ListView 使用刚才写的样式,运行代码可以看到下面图片
<ListView ItemsSource="{Binding Items}"
ItemContainerStyle="{StaticResource ListViewItemStyle}">
<ListView.ItemTemplate>
<DataTemplate DataType="local:Item">
<StackPanel>
<TextBlock Text="{Binding Id}" />
<TextBlock Text="{Binding Text}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>https://stackoverflow.com/a/53557393/6116637
边栏推荐
- 【商家联盟】云平台—异业联盟,打造线上线下商业相结合的系统
- How to convert an int attribute into a string in the json format returned by the Go language gin framework?
- 湖北电信天邑TY1608_S905L3B_MT7668_卡刷固件包
- 浙江移动咪咕MGV2000-K4_ZJ_S905l2_7661_线刷固件包
- 抖音最重要的接口——item_search_video-根据关键词获取视频列表
- 广东湛江海关破获3起走私冻海产品案 查证案值约1亿元
- redis
- 咪咕MGV2000KL南传_S905L3B_MT7668线刷固件包
- 适配器模式
- 域名哪家便宜?怎么买便宜域名?
猜你喜欢

移动平台助力推进智慧型科研院所信息化建设

“敏捷欺骗了开发人员”

湖北移动中兴B860AV2.1_S905L_线刷固件包

会话劫持安全攻击

Mobile magic box CM211-1_YS foundry _S905L3B_RTL8822C_wire brush firmware package

Heilongjiang Mobile New Magic Hundred Box M411A_2+8_S905L3A_wire brush firmware package

接口测试项目(非常值得练手)

"Distributed cloud best practices" BBS, on August 11, shenzhen

湖北移动HG680-LV_S905L3B_线刷固件包

Mobile Hisense IP102H_905L3-B_wire brush firmware package
随机推荐
从-99打造Sentinel高可用集群限流中间件
【商家联盟】云平台—异业联盟,打造线上线下商业相结合的系统
机器学习(十九):梯度提升回归(GBR)
智慧场馆的功能有哪些
花 30 美金请 AI 画家弄了个 logo,网友:画得非常好,下次别画了!
机器学习(十一):KNN(K近邻)
B站回应HR称核心用户是Loser;微博回应宕机原因;Go 1.19 正式发布|极客头条
华硕win11安全启动如何开启
ping不通百度
聚合收款码有限制吗?怎么办理?
LeetCode 1403.非递增顺序的最小子序列
"Distributed cloud best practices" BBS, on August 11, shenzhen
美容院管理系统有哪些促销方式?
Minecraft HMCL 使用认证服务器LittleSkin进行登录
gcc7.5.0编译ceres-solver报错‘is_trivially_default_constructible’ is not a member of ‘std’
湖北移动中兴B860AV2.1_S905L_线刷固件包
测试开发必备技能-Jmeter二次开发
湖北电信天邑TY1608_S905L3B_MT7668_卡刷固件包
码蹄集 - MT2094 - 回文之时:第4组数据错误
为什么买域名必须实名认证?这样做什么原因?