WPF的ListView控件自定义布局用法实例
本文实例讲述了WPF的ListView控件自定义布局用法。分享给大家供大家参考,具体如下:
概要:
以源码的形式贴出,免得忘记后,再到网上查资料。在VS2008+SP1环境下调试通过
引用的GrayscaleEffect模块,可根据参考资料《GrayscaleEffect...》中的位置下载。
正文:
如何布局是在App.xaml中定义源码如下
<Applicationx: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的布局模板-->
<ControlTemplatex:Key="myLVTemplate"TargetType="{x:TypeListView}">
<Grid>
<ScrollViewerx:Name="ScrollViewerElement"
Background="White"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Disabled">
<ItemsPresenter>
</ItemsPresenter>
</ScrollViewer>
</Grid>
</ControlTemplate>
<!--ListViewItem的布局模板-->
<DataTemplatex:Key="myLVItemTemplate">
<GridName="myGrid"Width="70"Margin="8,8,0,0">
<Grid.RowDefinitions>
<RowDefinitionHeight="Auto"></RowDefinition>
<RowDefinitionHeight="Auto"></RowDefinition>
</Grid.RowDefinitions>
<ImageName="img"Source="{BindingPath=ISource}"HorizontalAlignment="Center"
Width="32"Height="32"Stretch="UniformToFill"
ToolTip="{BindingPath=FullFileName}">
<Image.Effect>
<effect:GrayscaleEffectx:Name="grayscaleEffect"DesaturationFactor="1.0"/>
</Image.Effect>
</Image>
<TextBlockName="imgTitle"Text="{BindingPath=FileName}"Grid.Row="1"HorizontalAlignment="Center"
FontSize="10"FontFamily="Arial"
ToolTip="{BindingPath=FullFileName}"Margin="4,4,4,4"TextTrimming="CharacterEllipsis"/>
</Grid>
<DataTemplate.Triggers>
<DataTriggerBinding="{BindingRelativeSource={RelativeSourceMode=FindAncestor,
AncestorType={x:TypeListViewItem}},Path=IsSelected}"Value="True">
<SetterTargetName="myGrid"Property="Background"Value="White"/>
<SetterTargetName="imgTitle"Property="Background"Value="CadetBlue"/>
<SetterTargetName="imgTitle"Property="Foreground"Value="White"/>
<SetterTargetName="img"Property="Effect">
<Setter.Value>
<effect:GrayscaleEffectDesaturationFactor="0.5"/>
</Setter.Value>
</Setter>
<!--
<SetterTargetName="img"Property="Opacity"Value=".5"></Setter>
-->
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<!--下面这段代码不用,选中的时候边框有些边会变成蓝色(不是你希望的颜色)-->
<StyleTargetType="{x:TypeListViewItem}"x:Key="ItemContainerStyle">
<SetterProperty="Template">
<Setter.Value>
<ControlTemplateTargetType="{x:TypeListViewItem}">
<Borderx:Name="Bd"Background="{TemplateBindingBackground}"
BorderBrush="{TemplateBindingBorderBrush}"BorderThickness="0">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<TriggerProperty="IsSelected"Value="true">
<SetterProperty="Background"TargetName="Bd"Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
如何引用在Windows1.xaml中定义源码如下
<ListViewx:Name="listView1"ItemTemplate="{StaticResourcemyLVItemTemplate}"
Template="{StaticResourcemyLVTemplate}"ItemContainerStyle="{StaticResourceItemContainerStyle}"
Margin="0,4,0,4"MouseDoubleClick="OnLocalFSOpen"Grid.Row="2">
<!--下面定义WarpPanel,使Item项在容器里从左到右从上到下排列-->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ListView>
参考资料:
>>WPFListBoxTutorial
http://www.c-sharpcorner.com/uploadfile/mahesh/listbox-in-wpf/
>>Draganddrop,cut/copyandpastefileswithWindowsExplorer
http://www.codeproject.com/Articles/14059/Drag-and-drop-cut-copy-and-paste-files-with-Window
>>DataTemplatingOverview
http://msdn.microsoft.com/en-us/library/ms742521.aspx
>>WPFListBoxSelectionColor
http://stackoverflow.com/questions/794792/wpf-listbox-selection-color
>>WPFTutorial-UsingTheListView,Part3-InPlaceEdit
http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-3-in-place-edit
>>CreatingaCustomMarkupExtensioninWPF(andsoon,Silverlight)
http://10rem.net/blog/2011/03/09/creating-a-custom-markup-extension-in-wpf-and-soon-silverlight
>>MoreWPFCustomEffects:MotionBlurandGrayscaleSamples
http://windowsclient.net/wpf/wpf35/wpf-35sp1-more-effects.aspx
>>WPFTutorial-ControlsandLayout
http://www.mini.pw.edu.pl/~mossakow/materials/presentations/wpf.3.5/controls_layout/index.html
>>GrayscaleEffect-APixelShaderEffectinWPF
http://bursjootech.blogspot.com/2008/06/grayscale-effect-pixel-shader-effect-in.html
>>ShaderPad-WPFShaderEffects
http://shaderpad.codeplex.com/
更多关于C#相关内容感兴趣的读者可查看本站专题:《C#窗体操作技巧汇总》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#程序设计之线程使用技巧总结》、《C#操作Excel技巧总结》、《C#中XML文件操作技巧汇总》、《C#数据结构与算法教程》、《C#数组操作技巧总结》及《C#面向对象程序设计入门教程》
希望本文所述对大家C#程序设计有所帮助。