欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-08-29 21:37:40
本文实例讲述了wpf的listview控件自定义布局用法。分享给大家供大家参考,具体如下: 概要: 以源码的形式贴出,免得忘记后,再到网上查资料。在vs2008+sp1...

本文实例讲述了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://*.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#程序设计有所帮助。