当前位置: 首页 > 编程笔记 >

WPF的ListView控件自定义布局用法实例

暨宸
2023-03-14
本文向大家介绍WPF的ListView控件自定义布局用法实例,包括了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 ListView控件的实例详解,包括了C# WPF ListView控件的实例详解的使用技巧和注意事项,需要的朋友参考一下 C# WPF ListView控件的实例详解 C#的WPF作为现在微软主流的桌面程序开发平台,相比过去的MFC时代,有了非常多的不同。本人刚从MFC平台转过来,以为可以轻松上手,哪知碰到了很多问题,十分不解。不得不乖乖回去看了本书,再继续回到边左边

  • 问题内容: 我真的很想把头发拔出来。一些背景。我有一个项目列表,所有项目旁边都有复选框。取消选择复选框后,将出现一个按钮,您可以从列表中删除该项目。乍一看似乎是倒退,但我们只希望“选定”的项目有资格进行进一步处理,等等。这是我的布局: 我无法使这3个物品在行中垂直居中以挽救生命。,,,没有它的工作原理。我敢肯定,我的问题是要在某个地方翻转一下,但我确实对此持怀疑态度。 编辑:我知道textview

  • 有没有办法为不同的日志级别打印不同的布局?例如: 记录器。警告(“消息”);打印如下内容:2016-06-20 13:34:41245 INFO(main:)Message and for logger。信息(“消息2”);仅打印:消息2 有可能做到吗?定义一个布局以警告其他布局以获取信息 log4j.properties

  • 我正在使用PreferenceActivity设置我的应用程序。我想添加一个新的首选项,允许用户选择一个图标。对于这个任务,我想使用ListPreference,但我也想在列表中显示图标。 我尝试自定义List首选项以使用自定义布局,但问题是一旦我这样做了,列表项就不可单击(它确实显示了我的自定义布局并使用当前选择的默认值)。 我在不同的模拟器版本和银河S2上测试了它。当按下项目时,我可以看到一些

  • 本文向大家介绍IOS实现自定义布局瀑布流,包括了IOS实现自定义布局瀑布流的使用技巧和注意事项,需要的朋友参考一下 瀑布流是电商应用展示商品通常采用的一种方式,如图示例 瀑布流的实现方式,通常有以下几种 通过UITableView实现(不常用) 通过UIScrollView实现(工作量较大) 通过UICollectionView实现(通常采用的方式) 一、UICollectionView基础 1、