WPF的ListView控件自定义布局用法实例
2024-10-21 05:52:47
正文:
如何布局是在App.xaml中定义源码如下
< Application x:Class = "CWebsSynAssistant.App" xmlns:effect = "clr-namespace:GrayscaleEffect;assembly=GrayscaleEffect" StartupUri = "Window1.xaml" > < Application.Resources > <!-- ListView的布局模板--> < ControlTemplate x:Key = "myLVTemplate" TargetType = "{x:Type ListView}" > < Grid > < ScrollViewer x:Name = "ScrollViewerElement" Background = "White" VerticalScrollBarVisibility = "Auto" HorizontalScrollBarVisibility = "Disabled" > < ItemsPresenter > </ ItemsPresenter > </ ScrollViewer > </ Grid > </ ControlTemplate > <!-- ListViewItem的布局模板--> < DataTemplate x:Key = "myLVItemTemplate" > < Grid Name = "myGrid" Width = "70" Margin = "8,8,0,0" > < Grid.RowDefinitions > < RowDefinition Height = "Auto" ></ RowDefinition > < RowDefinition Height = "Auto" ></ RowDefinition > </ Grid.RowDefinitions > < Image Name = "img" Source = "{Binding Path=ISource}" HorizontalAlignment = "Center" Width = "32" Height = "32" Stretch = "UniformToFill" ToolTip = "{Binding Path=FullFileName}" > < Image.Effect > < effect:GrayscaleEffect x:Name = "grayscaleEffect" DesaturationFactor = "1.0" /> </ Image.Effect > </ Image > < TextBlock Name = "imgTitle" Text = "{Binding Path=FileName}" Grid.Row = "1" HorizontalAlignment = "Center" FontSize = "10" FontFamily = "Arial" ToolTip = "{Binding Path=FullFileName}" Margin = "4,4,4,4" TextTrimming = "CharacterEllipsis" /> </ Grid > < DataTemplate.Triggers > < DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode = FindAncestor , AncestorType={x:Type ListViewItem}}, Path = IsSelected }" Value = "True" > < Setter TargetName = "myGrid" Property = "Background" Value = "White" /> < Setter TargetName = "imgTitle" Property = "Background" Value = "CadetBlue" /> < Setter TargetName = "imgTitle" Property = "Foreground" Value = "White" /> < Setter TargetName = "img" Property = "Effect" > < Setter.Value > < effect:GrayscaleEffect DesaturationFactor = "0.5" /> </ Setter.Value > </ Setter > <!-- <Setter TargetName="img" Property="Opacity" Value=".5"></Setter> --> </ DataTrigger > </ DataTemplate.Triggers > </ DataTemplate > <!--下面这段代码不用,选中的时候边框有些边会变成蓝色(不是你希望的颜色)--> < Style TargetType = "{x:Type ListViewItem}" x:Key = "ItemContainerStyle" > < Setter Property = "Template" > < Setter.Value > < ControlTemplate TargetType = "{x:Type ListViewItem}" > < Border x:Name = "Bd" Background = "{TemplateBinding Background}" BorderBrush = "{TemplateBinding BorderBrush}" BorderThickness = "0" > < ContentPresenter /> </ Border > < ControlTemplate.Triggers > < Trigger Property = "IsSelected" Value = "true" > < Setter Property = "Background" TargetName = "Bd" Value = "White" /> </ Trigger > </ ControlTemplate.Triggers > </ ControlTemplate > </ Setter.Value > </ Setter > </ Style > </ Application.Resources > </ Application > |
如何引用在Windows1.xaml中定义源码如下
1
2
3
4
5
6
7
8
9
10
|
< ListView x:Name = "listView1" ItemTemplate = "{StaticResource myLVItemTemplate}" Template = "{StaticResource myLVTemplate}" ItemContainerStyle = "{StaticResource ItemContainerStyle}" Margin = "0,4,0,4" MouseDoubleClick = "OnLocalFSOpen" Grid.Row = "2" > <!--下面定义WarpPanel,使Item项在容器里从左到右从上到下排列--> < ItemsControl.ItemsPanel > < ItemsPanelTemplate > < WrapPanel /> </ ItemsPanelTemplate > </ ItemsControl.ItemsPanel > </ ListView > |
最新文章
- RequireJS基础(三)
- bootstrap源码分析之form、navbar
- AI,DM,ML,PR的区别与联系
- ASP.NET MVC- 在Area里使用RedirectToAction跳转出错的解决方法
- 了解运行时类型信息(RTTI)
- 转:完善eclipse+pdt作php开发中的代码提示能力
- Unity跨平台原理
- Spring框架 jar包下载
- win8在安装office visio2003出现“请求的操作需要提升”,解决方法
- Java进阶(三十七)java 自动装箱与拆箱
- Java记事本编写第一个程序“你好,世界”
- Shell编程(三)Shell特性
- MySQL数据库之安装
- eclipse4.2版本下面安装ADT,安装已经完成了,但没有ADT的那个图标显示
- (一) 天猫精灵接入Home Assistant- hass对接天猫精灵
- 右键菜单添加git选项
- shell 函数调用
- 在Windows Phone项目中调用C语言DLL
- freemarker多个checkbox一个被选中示例
- Spring IoC入门
热门文章
- 三个div向左浮动不在同一行,向右浮动在同一行的解决办法
- 2016百度之星资格赛 Round1(2,3,4题)
- 项目中使用libsodium扩展
- (4)django mtv模式
- 8大排序算法的java实现--做个人收藏
- CSU 1505: 酷酷的单词【字符串】
- android wifi 调试
- struts2设置默认首页
- Hadoop Mapreduce分区、分组、二次排序过程详解
- java wait(),notify(),notifyAll()的理解