WPF的ListView控件自定义布局用法实例
本文实例讲述了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#相关内容感兴趣的读者可查看本站专题:《C#窗体操作技巧汇总》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#程序设计之线程使用技巧总结》、《C#操作Excel技巧总结》、《C#中XML文件操作技巧汇总》、《C#数据结构与算法教程》、《C#数组操作技巧总结》及《C#面向对象程序设计入门教程》
希望本文所述对大家C#程序设计有所帮助。
相关文章
c# WPF中通过双击编辑DataGrid中Cell的示例(附源码)
这篇文章主要介绍了c# WPF中通过双击编辑DataGrid中Cell的示例(附源码),帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下...2021-03-03- 最近在网上看到了新版的360安全卫士,感觉界面还不错,于是用WPF制作了一个,时间有限,一些具体的控件没有制作,用图片代替了。感兴趣的朋友一起跟着小编学习WPF实现类似360安全卫士界面的程序源码分享...2020-06-25
- 这篇文章主要介绍了C#实现带进度条的ListView 的相关资料,需要的朋友可以参考下...2020-06-25
- 下面小编就为大家带来一篇C#获取鼠标在listview右键点击单元格的内容方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
C#实现读取DataSet数据并显示在ListView控件中的方法
这篇文章主要介绍了C#实现读取DataSet数据并显示在ListView控件中的方法,涉及C#操作DataSet及ListView控件的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25- 这篇文章主要为大家详细介绍了WPF仿三星手机充电界面实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要介绍了C#中WPF ListView绑定数据的实例详解的相关资料,希望通过本文能帮助到大家,让大家理解掌握这部分内容,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了C# WPF 通过委托实现多窗口间的传值的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要介绍了C# WPF ListView控件的实例详解的相关资料,希望通过本能帮助到大家,让大家掌握这部分内容,需要的朋友可以参考下...2020-06-25
- 首先 你需要添加引用Microsoft Excel 11.0 Object Library...2020-06-25
C# listview添加combobox到单元格的实现代码
从别处转来的,自己进行了一些小的修改,还不错,你自己先拖一个ListView1和一个ComboBox1,需要的朋友可以参考下...2020-06-25- 这篇文章主要介绍了C#中WPF使用多线程调用窗体组件的方法,涉及C#中多线程的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 这篇文章主要为大家详细介绍了WPF TextBox实现按字节长度限制输入功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要为大家详细介绍了WPF InkCanvas绘制矩形和椭圆,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要给大家介绍了关于WPF基础教程之形状画刷与变换的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要给大家介绍了关于WPF如何自定义TabControl控件样式的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。...2020-06-25
安卓开发使用定制的ArrayAdapter制作ListView的Items
下面我们来看一篇关于使用定制的ArrayAdapter制作ListView的Items的例子,希望这个例子能够给各位同学带来帮助的哦。 背景介绍 对于现实世界中的商业移动应用...2016-09-20- 下面我们来看一篇关于ListView移动到指定位置的例子吧,希望文章能够给各位朋友带来帮助,具体的如下介绍. 关于ListView移动到指定位置有两种方法 listview.setSel...2016-11-01
- ListView没有Item的双击事件,只能通过变通的方法得到,以下是我的变通方法...2020-06-25
- 想实现一个功能,这个功能挺简单,就是当不是管理员用户登入的时候,那这个 删除 按钮就被隐藏掉;当是管理员用户的时候,就重新显示出来...2021-09-22