背水一战 Windows 10 (58) - 控件(集合类): ListViewBase - ListView, GridView
作者:webabcd
介绍
背水一战 Windows 10 之 控件(集合类 - ListViewBase)
- ListView
- GridView
示例
1、ListView 的示例
Controls/CollectionControl/ListViewBaseDemo/ListViewDemo.xaml
<Page
x:Class="Windows10.Controls.CollectionControl.ListViewDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.CollectionControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" xmlns:common="using:Windows10.Common"> <Page.Resources>
<Style x:Key="ListViewItemStyle" TargetType="ListViewItem">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<!--
ListViewItemPresenter - ListViewItem 的 Presenter(继承自 ContentPresenter, 请参见 /Controls/BaseControl/ContentControlDemo/ContentPresenterDemo.xaml)
有好多属性,详见文档
默认样式就是 generic.xaml 中的 <Style TargetType="ListViewItem"> 节点
如果需要自定义的话,那么就在 generic.xaml 中的 <Style TargetType="ListViewItem" x:Key="ListViewItemExpanded"> 节点的基础上修改
如果还不能满足要求的话就通过继承 ContentPresenter 来实现自定义的 ContentPresenter
-->
<!--
此处的 TemplatedParent 是 ListViewItem
这里借用 Tag 保存一下 ListViewItem 的 IsSelected,之后的数据模板可以绑定 ListViewItemPresenter 的 Tag,从而实现数据模板间接绑定 ListViewItem 的 IsSelected
此处通过 Tag 属性做中转,如果 Tag 有别的用处的话,那么就自己写个附加属性做中转吧
-->
<ListViewItemPresenter Margin="10" SelectedBackground="Red" SelectedPointerOverBackground="Red"
Tag="{Binding IsSelected, RelativeSource={RelativeSource Mode=TemplatedParent}, Mode=TwoWay}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<common:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Page.Resources> <Grid Background="Transparent"> <!--
ListView - ListView 控件(继承自 ListViewBase, 请参见 /Controls/CollectionControl/ListViewBaseDemo/)
ListView 的默认布局控件是 ItemsStackPanel,请参见 /Controls/CollectionControl/ItemsControlDemo/LayoutControl/ItemsStackPanelDemo.xaml
ListView 的 ItemContainer 是 ListViewItem
--> <ListView x:Name="listView" Margin="10 0 10 10"
ItemContainerStyle="{StaticResource ListViewItemStyle}"
SelectionMode="Multiple"
ItemsSource="{x:Bind Employees}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="common:Employee">
<StackPanel Height="100" Width="100" Background="Blue">
<TextBlock x:Name="lblName" Text="{x:Bind Name}" />
<TextBlock x:Name="lblIsMale" Text="{x:Bind IsMale}" />
<!--
这里有个需求:当 ListViewItem 的 IsSelected 为 true 时显示,反之则不显示
此处的 TemplatedParent 是 ListViewItemPresenter,而不是 ListViewItem,所以需要 ListViewItemPresenter 中转一下(ListViewItemPresenter 的 TemplatedParent 是 ListViewItem)
此处通过 Tag 属性做中转,如果 Tag 有别的用处的话,那么就自己写个附加属性做中转吧 如果以后 uwp 支持了 FindAncestor 的话,就可以不用中转了,直接这样写就行了
{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}}
-->
<TextBlock x:Name="lblAge" Text="{x:Bind Age}"
Visibility="{Binding Path=Tag, RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource BooleanToVisibilityConverter}}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView> <!--
通过 xaml 方式为 ListView 添加 item
<ListView>
<ListViewItem>
<TextBlock Text="item1"/>
</ListViewItem>
<ListViewItem>
<TextBlock Text="item2"/>
</ListViewItem>
<ListViewItem>
<TextBlock Text="item3"/>
</ListViewItem>
</ListView>
--> </Grid>
</Page>
Controls/CollectionControl/ListViewBaseDemo/ListViewDemo.xaml.cs
/*
* ListView - ListView 控件(继承自 ListViewBase, 请参见 /Controls/CollectionControl/ListViewBaseDemo/)
*/ using System.Collections.ObjectModel;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows10.Common; namespace Windows10.Controls.CollectionControl
{
public sealed partial class ListViewDemo : Page
{
public ObservableCollection<Employee> Employees { get; set; } = new ObservableCollection<Employee>(TestData.GetEmployees()); public ListViewDemo()
{
this.InitializeComponent();
}
}
}
2、GridView 的示例
Controls/CollectionControl/ListViewBaseDemo/GridViewDemo.xaml
<Page
x:Class="Windows10.Controls.CollectionControl.GridViewDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.CollectionControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" xmlns:common="using:Windows10.Common"> <Page.Resources>
<Style x:Key="GridViewItemStyle" TargetType="GridViewItem">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GridViewItem">
<!--
ListViewItemPresenter - GridViewItem 的 Presenter(继承自 ContentPresenter, 请参见 /Controls/BaseControl/ContentControlDemo/ContentPresenterDemo.xaml)
默认样式就是 generic.xaml 中的 <Style TargetType="GridViewItem"> 节点
如果需要自定义的话,那么就在 generic.xaml 中的 <Style TargetType="GridViewItem" x:Key="GridViewItemExpanded"> 节点的基础上修改
如果还不能满足要求的话就通过继承 ContentPresenter 来实现自定义的 ContentPresenter
-->
<ListViewItemPresenter Margin="10" SelectionCheckMarkVisualEnabled="True" SelectedBackground="Red" CheckBrush="Yellow" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources> <Grid Background="Transparent"> <!--
GridView - GridView 控件(继承自 ListViewBase, 请参见 /Controls/CollectionControl/ListViewBaseDemo/)
GridView 的默认布局控件是 ItemsWrapGrid,请参见 /Controls/CollectionControl/ItemsControlDemo/LayoutControl/ItemsWrapGridDemo.xaml
GridView 的 ItemContainer 是 GridViewItem
--> <GridView x:Name="gridView" Margin="10 0 10 10"
ItemContainerStyle="{StaticResource GridViewItemStyle}"
SelectionMode="Multiple"
ItemsSource="{x:Bind Employees}">
<GridView.ItemTemplate>
<DataTemplate x:DataType="common:Employee">
<StackPanel Height="100" Width="100" Background="Blue">
<TextBlock x:Name="lblName" Text="{x:Bind Name}" Foreground="Yellow" />
<TextBlock x:Name="lblAge" Text="{x:Bind Age}" Foreground="Aqua" />
<TextBlock x:Name="lblIsMale" Text="{x:Bind IsMale}" Foreground="Gray" />
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView> <!--
通过 xaml 方式为 GridView 添加 item
<GridView>
<GridViewItem>
<TextBlock Text="item1"/>
</GridViewItem>
<GridViewItem>
<TextBlock Text="item2"/>
</GridViewItem>
<GridViewItem>
<TextBlock Text="item3"/>
</GridViewItem>
</GridView>
--> </Grid>
</Page>
Controls/CollectionControl/ListViewBaseDemo/GridViewDemo.xaml.cs
/*
* GridView - GridView 控件(继承自 ListViewBase, 请参见 /Controls/CollectionControl/ListViewBaseDemo/)
*/ using System.Collections.ObjectModel;
using Windows.UI.Xaml.Controls;
using Windows10.Common; namespace Windows10.Controls.CollectionControl
{
public sealed partial class GridViewDemo : Page
{
public ObservableCollection<Employee> Employees { get; set; } = new ObservableCollection<Employee>(TestData.GetEmployees()); public GridViewDemo()
{
this.InitializeComponent();
}
}
}
OK
[源码下载]
背水一战 Windows 10 (58) - 控件(集合类): ListViewBase - ListView, GridView的更多相关文章
- 背水一战 Windows 10 (56) - 控件(集合类): ListViewBase - 基础知识, 拖动项
[源码下载] 背水一战 Windows 10 (56) - 控件(集合类): ListViewBase - 基础知识, 拖动项 作者:webabcd 介绍背水一战 Windows 10 之 控件(集合 ...
- 背水一战 Windows 10 (57) - 控件(集合类): ListViewBase - 增量加载, 分步绘制
[源码下载] 背水一战 Windows 10 (57) - 控件(集合类): ListViewBase - 增量加载, 分步绘制 作者:webabcd 介绍背水一战 Windows 10 之 控件(集 ...
- 背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation
[源码下载] 背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation 作者:webabcd 介绍背水一战 Wind ...
- 背水一战 Windows 10 (48) - 控件(集合类): FlipView
[源码下载] 背水一战 Windows 10 (48) - 控件(集合类): FlipView 作者:webabcd 介绍背水一战 Windows 10 之 控件(集合类) FlipView 示例Fl ...
- 背水一战 Windows 10 (49) - 控件(集合类): Pivot, Hub
[源码下载] 背水一战 Windows 10 (49) - 控件(集合类): Pivot, Hub 作者:webabcd 介绍背水一战 Windows 10 之 控件(集合类) Pivot Hub 示 ...
- 背水一战 Windows 10 (50) - 控件(集合类): ItemsControl - 基础知识, 数据绑定, ItemsPresenter, GridViewItemPresenter, ListViewItemPresenter
[源码下载] 背水一战 Windows 10 (50) - 控件(集合类): ItemsControl - 基础知识, 数据绑定, ItemsPresenter, GridViewItemPresen ...
- 背水一战 Windows 10 (51) - 控件(集合类): ItemsControl - 项模板选择器, 数据分组
[源码下载] 背水一战 Windows 10 (51) - 控件(集合类): ItemsControl - 项模板选择器, 数据分组 作者:webabcd 介绍背水一战 Windows 10 之 控件 ...
- 背水一战 Windows 10 (52) - 控件(集合类): ItemsControl - 自定义 ItemsControl, 自定义 ContentPresenter
[源码下载] 背水一战 Windows 10 (52) - 控件(集合类): ItemsControl - 自定义 ItemsControl, 自定义 ContentPresenter 作者:weba ...
- 背水一战 Windows 10 (53) - 控件(集合类): ItemsControl 的布局控件 - ItemsStackPanel, ItemsWrapGrid
[源码下载] 背水一战 Windows 10 (53) - 控件(集合类): ItemsControl 的布局控件 - ItemsStackPanel, ItemsWrapGrid 作者:webabc ...
随机推荐
- Tomcat+Redis+Nginx实现session共享(Windows版)
redis安装:xx nginx安装:xx 步骤: 1.下载tomcat-redis-session-manager相应的jar包,主要有三个: wget https://github.com/dow ...
- spring bean 生命周期和 ? 作用域? spirng bean 相互依赖? jvm oom ? jvm 监控工具? ThreadLocal 原理
1. spring bean 生命周期 1. 实例化一个bean ,即new 2. 初始化bean 的属性 3. 如果实现接口 BeanNameAware ,调用 setBeanName 4. Bea ...
- ajax post 请求 ,java端使用 request.getParameter 获取不到数据问题
js端 $.ajax({ type:'POST', data:{a:1}, url:_this.apiUrl+url, dataType:'json',//使用jsonp方式请求 contentTyp ...
- 2017-2018-1 20155312《信息安全技术》实验二——Windows口令破解实验报告
2017-2018-1 20155312<信息安全技术>实验二--Windows口令破解实验报告 实验目的 了解Windows口令破解原理 对信息安全有直观感性认识 能够运用工具实现口令破 ...
- JNI,RegisterNative参数解析
Register native method - 数据类型和method descriptor 使用JNI时,为了使得虚拟机可以找到在C/C++ code中定义的native方法,有两种机制可以用,一 ...
- swift -inout关键字
一般参数仅仅是在函数内可以改变的,当这个函数执行完后变量就会被销毁,不会有机会改变函数以外的变量,那么我们就会产生一个疑问,我们可不可以通过一个函数改变函数外面变量的值呢?答案是肯定的,这时我们就需要 ...
- mysql5.6优化
下面开始优化下my.conf文件(这里的优化只是在mysql本身的优化,之前安装的时候也要有优化) cat /etc/my.cnf # For advice on how to change sett ...
- DevOps:软件架构师行动指南(文摘)
第一部分 背景 第1章 DevOps是什么 第二部分 部署流水线 第三部分 横切关注点 第四部分 案例研究 第五部分 走向未来
- vue 开发系列(四) vue 使用外部JS
概要 在开发时我们会经常需要使用到外部的JS,这样我们需要引入外部js,然后进行使用. 实现方法 我们在开发的过程中需要使用到 sha256 将用户的密码进行加密传输. 我们对js进行一点点改造. f ...
- Mybatis-Plus 实战完整学习笔记(六)------select测试一
查询方法(3.0.3) 1.查询一个员工的数据 @Test public void selectMethod() throws SQLException { // 根据ID获取一个对象的数据 Empl ...