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#程序设计有所帮助。

标签:WPF,ListView
0
投稿

猜你喜欢

  • Java 实战项目锤炼之小区物业管理系统的实现流程

    2021-07-17 09:24:01
  • android实现文件读写功能

    2022-03-10 16:31:23
  • android实现倒计时动态圈

    2023-09-15 03:18:33
  • java.lang.Void类源码解析

    2022-07-15 04:07:46
  • C++中auto_ptr智能指针的用法详解

    2023-05-21 02:14:05
  • Unity实现菜品识别的示例代码

    2023-06-11 22:16:10
  • Java中的MessageFormat.format用法实例

    2022-06-23 23:31:37
  • 通过与Java功能上的对比来学习Go语言

    2023-02-18 02:04:53
  • c# 成员类型访问权限低于字段本身的实现

    2021-12-23 07:08:27
  • Android自定义view之围棋动画效果的实现

    2022-05-07 17:36:38
  • SpringMVC 传日期参数到后台的实例讲解

    2022-01-18 23:31:48
  • C# GDI+实现时钟表盘

    2023-06-20 07:11:32
  • Java中对List集合的常用操作详解

    2023-05-02 23:52:47
  • Android资源文件与层次式导航超详细讲解

    2022-07-14 16:28:22
  • springmvc不进入Controller导致404的问题

    2023-03-18 01:29:20
  • android预置默认的语音信箱号码具体实现

    2022-08-22 07:51:59
  • 通过实例了解java spring使用构造器注入的原因

    2022-11-25 03:48:24
  • Android自定义DigitalClock控件实现商品倒计时

    2023-07-17 20:13:28
  • Cocos2d-x入门教程(详细的实例和讲解)

    2023-07-31 17:18:00
  • spring MVC实现简单登录功能

    2023-11-13 05:20:48
  • asp之家 软件编程 m.aspxhome.com