WPF ListBox
参考:
https://www.cnblogs.com/callyblog/p/8044963.html
https://www.cnblogs.com/zhoujg/archive/2009/12/04/1616713.html
1 2 3 4 5 6 7 | < Window.Resources > <!-- 启用虚拟化 --> < Style TargetType="{x:Type ListBox}"> < Setter Property="VirtualizingPanel.IsVirtualizing" Value="True"/> < Setter Property="VirtualizingPanel.VirtualizationMode" Value="Recycling"/> </ Style > </ Window.Resources > |
启用虚拟化
WPF ListBox中滑轮滚动步数调整,参考:https://www.cnblogs.com/wzwyc/p/16254317.html
WPF 列表自动换行
参考:https://www.codercto.com/a/58801.html
<ListBox Name= "antennaListBox" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text= "{Binding Path=Name}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> |
1、ListBox默认模板
![]()
|
![]()
|
![]()
|
2、ListBox自定义模板
![]()
|
![]()
|
![]()
|
3、ListBox默认列表模板
|
![]()
|
![]()
|
4、ListBox自定义列表模板
![]()
|
![]()
|
![]()
|
XAML 示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | < Style TargetType="ListBox" x:Key="ListBox"> < Setter Property="BorderBrush" Value="Red"/> < Setter Property="BorderThickness" Value="1"/> < Setter Property="Width" Value="280"/> < Setter Property="Height" Value="180"/> < Setter Property="Template"> < Setter.Value > < ControlTemplate TargetType="ListBox"> < Border BorderBrush="Green" BorderThickness="1"> < ItemsPresenter /> </ Border > </ ControlTemplate > </ Setter.Value > </ Setter > < Setter Property="ItemsPanel"> < Setter.Value > < ItemsPanelTemplate > < StackPanel /> </ ItemsPanelTemplate > </ Setter.Value > </ Setter > < Setter Property="ItemContainerStyle"> < Setter.Value > < Style TargetType="ListBoxItem"> < Setter Property="Template"> < Setter.Value > < ControlTemplate TargetType="ListBoxItem"> < Border x:Name="PART_Border" BorderBrush="Blue" BorderThickness="1" Height="40"> < ContentPresenter /> </ Border > </ ControlTemplate > </ Setter.Value > </ Setter > </ Style > </ Setter.Value > </ Setter > < Setter Property="ItemTemplate"> < Setter.Value > < DataTemplate > < TextBlock Text="{Binding title}"/> </ DataTemplate > </ Setter.Value > </ Setter > </ Style > |
在控件中设置样式
XAML 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | < Window ...> < Grid > < ListBox > < ListBox.ItemContainerStyle > < Style TargetType="{x:Type ListBoxItem}"> < Setter Property="OverridesDefaultStyle" Value="True" /> < Setter Property="SnapsToDevicePixels" Value="True" /> < Setter Property="Foreground" Value="Black" /> < Setter Property="Height" Value="40" /> < Setter Property="FontSize" Value="16" /> < Setter Property="VerticalContentAlignment" Value="Center" /> < Setter Property="Template"> < Setter.Value > < ControlTemplate TargetType="{x:Type ListBoxItem}"> < Border x:Name="border" CornerRadius="0" Margin="2" BorderBrush="Black" BorderThickness="0,0,0,0.2"> < ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="10,0,0,0" /> </ Border > < ControlTemplate.Triggers > < Trigger Property="IsSelected" Value="True"> < Setter Property="Foreground" Value="#FF46679A" /> < Setter TargetName="border" Property="Background" Value="white" /> < Setter TargetName="border" Property="BorderBrush" Value="#FF46679A" /> < Setter TargetName="border" Property="BorderThickness" Value="4,0,0,0.5" /> </ Trigger > < MultiTrigger > < MultiTrigger.Conditions > < Condition Property="IsMouseOver" Value="True" /> < Condition Property="IsSelected" Value="False" /> </ MultiTrigger.Conditions > < MultiTrigger.Setters > < Setter Property="Foreground" Value="#FF46679A" /> < Setter TargetName="border" Property="Background" Value="white" /> < Setter TargetName="border" Property="BorderBrush" Value="#FF46679A" /> < Setter TargetName="border" Property="BorderThickness" Value="0,0,0,0.5" /> </ MultiTrigger.Setters > </ MultiTrigger > </ ControlTemplate.Triggers > </ ControlTemplate > </ Setter.Value > </ Setter > </ Style > </ ListBox.ItemContainerStyle > < ListBoxItem >简单学习网</ ListBoxItem > < ListBoxItem >腾讯游戏</ ListBoxItem > < ListBoxItem >网易游戏</ ListBoxItem > < ListBoxItem >哔哩哔哩</ ListBoxItem > < ListBoxItem >敦刻尔克</ ListBoxItem > < ListBoxItem >必应搜索</ ListBoxItem > < ListBoxItem >京东商城</ ListBoxItem > </ ListBox > </ Grid > </ Window > |
ListBox横向排列
1 2 3 4 5 6 7 8 9 | < ListBox.Template > < ControlTemplate TargetType="{x:Type ListBox}"> < Border BorderBrush="#e4e4e4" BorderThickness="1"> < ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto"> < WrapPanel Orientation="Horizontal" IsItemsHost="True" ScrollViewer.CanContentScroll="True"/> </ ScrollViewer > </ Border > </ ControlTemplate > </ ListBox.Template > |
ListBox ListBoxItem 命令
<Button Content= "邀请" Height= "28" > <i:Interaction.Triggers> <i:EventTrigger EventName= "Click" > <i:InvokeCommandAction Command= "{Binding Path=DataContext.CommandSendPK,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ListBox}}" CommandParameter= "{Binding Content, RelativeSource ={RelativeSource AncestorType={x:Type ListBoxItem}}}" /> </i:EventTrigger> </i:Interaction.Triggers> </Button> |
默认显示最后一行
public class ScrollingListBox : ListBox { protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.NewItems!= null ) //此处需要判空 { int newItemCount = e.NewItems.Count; if (newItemCount > 0) this .ScrollIntoView(e.NewItems[newItemCount - 1]); base .OnItemsChanged(e); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了