WPF的ListView控件自定義布局用法實例
本文實例講述了WPF的ListView控件自定義布局用法。分享給大家供大家參考,具體如下:
概要:
以源碼的形式貼出,免得忘記后,再到網(wǎng)上查資料。在VS2008+SP1環(huán)境下調(diào)試通過
引用的GrayscaleEffect模塊,可根據(jù)參考資料《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/
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#窗體操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#程序設(shè)計之線程使用技巧總結(jié)》、《C#操作Excel技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》
希望本文所述對大家C#程序設(shè)計有所幫助。
相關(guān)文章
C#操作SQLite實現(xiàn)數(shù)據(jù)的增刪改查
SQLite是一個輕量級、跨平臺的關(guān)系型數(shù)據(jù)庫,在小型項目中,方便,易用,同時支持多種開發(fā)語言。本文將用C#語言對SQLite 的一個封裝,實現(xiàn)數(shù)據(jù)的增刪改查。需要的可以參考一下2022-01-01C#對圖片進行馬賽克處理可控制模糊程度的實現(xiàn)代碼
本文通過實例代碼給大家介紹了C#對圖片進行馬賽克處理可控制模糊程度的實現(xiàn)方法,代碼超簡單,具有一定的參考借鑒價值,感興趣的朋友跟隨腳本之家小編一起學習吧2018-05-05C#?Windows?Forms中實現(xiàn)控件之間的連接線的方法詳解
這篇文章主要為大家詳細介紹了如何在C#?Windows?Forms應(yīng)用程序中實現(xiàn)繪圖工具中多個控件之間的連接線功能,文中的示例代碼講解詳細,需要的可以參考下2024-02-02使用C# CefSharp Python采集某網(wǎng)站簡歷并且自動發(fā)送邀請短信的方法
這篇文章主要給大家介紹了關(guān)于如何使用C# CefSharp Python采集某網(wǎng)站簡歷并且自動發(fā)送邀請短信的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧2019-03-03C#判斷指定驅(qū)動器是否已經(jīng)準備就緒的方法
這篇文章主要介紹了C#判斷指定驅(qū)動器是否已經(jīng)準備就緒的方法,涉及C#針對硬件IO操作的技巧,需要的朋友可以參考下2015-04-04C#實現(xiàn)文件與字符串互轉(zhuǎn)的方法詳解
這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)文件與字符串互轉(zhuǎn)效果,文中的示例代碼講解詳細,對我們學習C#有一定幫助,需要的可以參考一下2022-08-08