WPF的ListView控件自定义布局用法实例
作者:kagula 发布时间:2021-09-04 12:15:45
本文实例讲述了WPF的ListView控件自定义布局用法。分享给大家供大家参考,具体如下:
概要:
以源码的形式贴出,免得忘记后,再到网上查资料。在VS2008+SP1环境下调试通过
引用的GrayscaleEffect模块,可根据参考资料《Grayscale Effect...》中的位置下载。
正文:
如何布局是在App.xaml中定义源码如下
<Application x:Class="CWebsSynAssistant.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
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中定义源码如下
<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>
参考资料:
>>WPF ListBox Tutorial
http://www.c-sharpcorner.com/uploadfile/mahesh/listbox-in-wpf/
>>Drag and drop, cut/copy and paste files with Windows Explorer
http://www.codeproject.com/Articles/14059/Drag-and-drop-cut-copy-and-paste-files-with-Window
>>Data Templating Overview
http://msdn.microsoft.com/en-us/library/ms742521.aspx
>>WPF ListBox Selection Color
http://stackoverflow.com/questions/794792/wpf-listbox-selection-color
>>WPF Tutorial - Using The ListView, Part 3 - In Place Edit
http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-3-in-place-edit
>>Creating a Custom Markup Extension in WPF (and soon, Silverlight)
http://10rem.net/blog/2011/03/09/creating-a-custom-markup-extension-in-wpf-and-soon-silverlight
>>More WPF Custom Effects: Motion Blur and Grayscale Samples
http://windowsclient.net/wpf/wpf35/wpf-35sp1-more-effects.aspx
>>WPF Tutorial - Controls and Layout
http://www.mini.pw.edu.pl/~mossakow/materials/presentations/wpf.3.5/controls_layout/index.html
>>Grayscale Effect - A Pixel Shader Effect in WPF
http://bursjootech.blogspot.com/2008/06/grayscale-effect-pixel-shader-effect-in.html
>>ShaderPad - WPF ShaderEffects
http://shaderpad.codeplex.com/
希望本文所述对大家C#程序设计有所帮助。


猜你喜欢
- 一、三方库介绍地址:https://github.com/youth5201314/banner介绍:从学习 Android 开始到现在工作
- 单元测试单元测试就是针对最小的功能单元编写测试代码,Java程序最小的功能单元是方法,因此,单元测试就是针对Java方法的测试,进而检查方法
- 写在前面jenkins作为java的好 * ,经历过单体项目时代->集群项目时代->容器集群分布式时代,使用稳定可靠,cpu友好(
- 一、场景描述建造者模式同工厂模式、抽象工厂模式一样,用于创建继承类对象。工厂模式:Java设计模式之工厂模式抽象工厂模式:Java设计模式之
- 【一】常见用法最原始的用法,耦合度低,但是不能统一管理。我们需要在每一个控制器都写以下代码,很繁琐,以后项目修改起来更繁琐,得一个控制器一个
- 一、设置Jackson序列化时只包含不为空的字段new ObjectMapper().setSerializationInclusion(I
- 在J2EE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的、不可预知的异
- 1、概述Spring Retry 是Spring框架中的一个组件,它提供了自动重新调用失败操作的能力。这在错误可能是暂时发生的(如瞬时网络故
- 项目中用到WebView加上进度条放在顶部,让用户知道加载进度情况,可以提高用户体验:效果:布局:<RelativeLayoutand
- 目前网上流行着很多对“时间对话框TimePickerDialog”的讲解文章,但感觉都不是很详细。所以浣熊在这里详细对该方面的知识进行介绍,
- springboot项目还是ssm等java常用框架都会有这样的问题,解决办法通用问题场景前端发送Post请求,前端返回400 Bad Re
- java @Value("${}")获取不到配置文件中值1、property.yml配置spring: ma
- 前言最近测试给我提了一个bug,说我之前提供的一个批量复制商品的接口,产生了重复的商品数据。追查原因之后发现,这个事情没想象中简单,可以说一
- 本文所述实例实现将一张图片上传到指定的文件夹,然后在窗体上的PictrueBox控件中显示出来。具体功能代码如下:private void
- 建库建表DROP DATABASE IF EXISTS mp;CREATE DATABASE mp DEFAULT CHARACTER SE
- public static class DataTableHelper { &nb
- 先上图下拉刷新跟原生开发一样,下拉刷新在flutter里提供的有组件实现 RefreshIndicator一直不明白为啥组件中都提供下拉刷新
- 前言其实不管是哪种滑动方式,基本思想都是类似的:当点击事件传递到View时,系统记下触摸点的坐标,手指移动的时候,系统记下移动后的坐标,并计
- 本文实例为大家分享Android通过Movie展示Gif格式图片的相关代码,供大家参考,具体内容如下public class CommonG
- 相信大家对 String 和 StringBuffer 的区别也已经很了解了,但是估计还是会有很多同志对这两个类的工作原理有些不清楚的地方,