title author date CreateTime categories
win10 UWP ListView
lindexi
2018-08-10 19:16:53 +0800
2018-2-13 17:23:3 +0800
Win10 UWP

横向布局

默认 ListView 是垂直,那么如何让 ListView 水平?

可以使用下面代码

            <ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>

设置代码可以进行横向。

如果发现 UWP ListView 横向没有滚动条,可以使用 ScrollViewer 添加

            <ListView  ScrollViewer.VerticalScrollBarVisibility="Disabled"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollMode="Enabled"
ScrollViewer.VerticalScrollMode="Disabled">

使用从左到右放元素

实际上 ItemsPanelTemplate 可以放很多个类型,如 WrapGrid 和 ItemsWrapGrid ,下面我告诉大家如何做出这个效果

  <ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid Orientation="Horizontal"></ItemsWrapGrid>
</ItemsPanelTemplate>
</ListView.ItemsPanel>

这时可以设置元素的宽度,或者高度,这样可以做出下面的效果。

选中显示元素

有一些元素是要 Item 选中显示,不选中不显示

如何绑定到Item 的状态,是否被选中?

如果可以写在后台代码多的话,一个简单的方法是在SelectionChanged直接让 AddItems 的显示,其他不显示。

如何想要定义样式,可以参见:https://msdn.microsoft.com/en-us/library/windows/apps/mt299136.aspx

首先把代码复制下来,然后修改 Selected 的动画,添加自己元素在ControlTemplate,看起来就是

                       <ControlTemplate TargetType="ListViewItem">
<Grid>
<ContentPresenter ></ContentPresenter>
<Button x:Name="b" Opacity="0" HorizontalAlignment="Center" Content="显示"></Button>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SelectionStates">
<!--<VisualState x:Name="Unselecting">
<Storyboard BeginTime="0:0:0">
<DoubleAnimation Storyboard.TargetName="b"
Storyboard.TargetProperty="Opacity"
Duration="0:0:0.1"
To="0" />
</Storyboard>
</VisualState>-->
<VisualState x:Name="Unselected">
<Storyboard BeginTime="0:0:0">
<DoubleAnimation Storyboard.TargetName="b"
Storyboard.TargetProperty="Opacity"
Duration="0"
To="0" />
</Storyboard>
</VisualState>
<VisualState x:Name="Selected">
<Storyboard BeginTime="0:0:0">
<DoubleAnimation Storyboard.TargetName="b"
Storyboard.TargetProperty="Opacity"
Duration="0"
To="1" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>

上面代码的元素 b 就是加上去的元素,参见他的做法,可以看到这个方法可以在 选择时显示,但是我无法在不选择时隐藏,原因没找到。

根据上面代码,可以做很小修改,在选择改变时,手动使用变化。

首先把 Selected 改为 CustomSelected 现在的代码换为

                        <ControlTemplate TargetType="ListViewItem">
<Grid>
<ContentPresenter ></ContentPresenter>
<Button x:Name="b" Opacity="0" HorizontalAlignment="Center" Content="显示"></Button>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SelectionStates">
<!--<VisualState x:Name="Unselecting">
<Storyboard BeginTime="0:0:0">
<DoubleAnimation Storyboard.TargetName="b"
Storyboard.TargetProperty="Opacity"
Duration="0:0:0.1"
To="0" />
</Storyboard>
</VisualState>-->
<VisualState x:Name="CustomUnselected">
<Storyboard BeginTime="0:0:0">
<DoubleAnimation Storyboard.TargetName="b"
Storyboard.TargetProperty="Opacity"
Duration="0"
To="0" />
</Storyboard>
</VisualState>
<VisualState x:Name="CustomSelected">
<Storyboard BeginTime="0:0:0">
<DoubleAnimation Storyboard.TargetName="b"
Storyboard.TargetProperty="Opacity"
Duration="0"
To="1" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>

在列表的选择改变时,需要在后台代码写

                var listView = (sender as ListView);
if (listView == null)
{
return;
}
if (e.AddedItems != null)
{
foreach (var item in e.AddedItems)
{
Debug.WriteLine(item);
ListViewItem litem = listView.ContainerFromItem(item) as ListViewItem;
if (litem != null)
{
VisualStateManager.GoToState(litem, "CustomSelected", true);
}
}
}
if (e.RemovedItems != null)
{
foreach (var item in e.RemovedItems)
{
Debug.WriteLine(item);
ListViewItem litem = listView.ContainerFromItem(item) as ListViewItem;
if (litem != null)
{
VisualStateManager.GoToState(litem, "CustomUnselected", true);
}
}
}

这个方法是比较差的,但是可以使用

参见:http://stackoverflow.com/questions/43461819/the-listviewitem-style-cant-trigger-unselected

ListViewItem 默认

<Style
TargetType="ListViewItem">

</Style>

WPF ListView 宽度

使用下面的代码可以让 WPF 的 ListView 的 Item 宽度和他一样

HorizontalContentAlignment="Stretch"

<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListBox.ItemContainerStyle>

How to get a ListBox ItemTemplate to stretch horizontally the full width of the ListBox? - Stack Overflow

2018-8-10-win10-UWP-ListView-的更多相关文章

  1. win10 uwp 列表模板选择器

    本文主要讲ListView等列表可以根据内容不同,使用不同模板的列表模板选择器,DataTemplateSelector. 如果在 UWP 需要定义某些列的显示和其他列不同,或者某些行的显示和其他行不 ...

  2. win10 uwp 商业游戏 1.1.5

    本文是在win10 uwp 商业游戏 基础上继续开发,添加一些无聊的游戏 因为在发布几个月,下载量很少,小伙伴说游戏就玩不到几分钟就不想玩,于是我就想加入其他游戏 下面我来告诉大家如何在游戏中添加多个 ...

  3. win10 uwp 毛玻璃

    毛玻璃在UWP很简单,不会和WPF那样伤性能. 本文告诉大家,如何在 UWP 使用 win2d 做毛玻璃. 毛玻璃可以使用 win2D 方法,也可以使用 Compositor . 使用 win2d 得 ...

  4. win10 uwp 商业游戏 1.2.1

    上一个游戏已经告诉大家如何写多个游戏,现在继续写这个无聊的游戏 希望大家在看这篇文章之前先看win10 uwp 商业游戏,在这个文章告诉了大家如何创建游戏. 修改数值 可以从上一篇的博客的游戏看到升级 ...

  5. win10 uwp 商业游戏

    本文告诉大家去做一个商业游戏,游戏很简单,几乎没有什么技术 游戏的开始,需要添加框架库,于是引用我自己写的库. 首先是创建一个启动页面,这个页面是显示启动的. 在显示启动的时候,是需要加载游戏需要使用 ...

  6. win10 uwp 手把手教你使用 asp dotnet core 做 cs 程序

    本文是一个非常简单的博客,让大家知道如何使用 asp dot net core 做后台,使用 UWP 或 WPF 等做前台. 本文因为没有什么业务,也不想做管理系统,所以看到起来是很简单. Visua ...

  7. win10 uwp 使用 Microsoft.Graph 发送邮件

    在 2018 年 10 月 13 号参加了 张队长 的 Office 365 训练营 学习如何开发 Office 365 插件和 OAuth 2.0 开发,于是我就使用 UWP 尝试使用 Micros ...

  8. Win10 UWP开发系列:实现Master/Detail布局

    在开发XX新闻的过程中,UI部分使用了Master/Detail(大纲/细节)布局样式.Win10系统中的邮件App就是这种样式,左侧一个列表,右侧是详情页面.关于这种 样式的说明可参看MSDN文档: ...

  9. Win10 UWP开发实现Bing翻译

    微软在WP上的发展从原来的Win7到Win8,Win8.1,到现在的Win10 UWP,什么是UWP,UWP即Windows 10 中的Universal Windows Platform简称.即Wi ...

  10. Win10/UWP开发—使用Cortana语音与App后台Service交互

    上篇文章中我们介绍了使用Cortana调用前台App,不熟悉的移步到:Win10/UWP开发—使用Cortana语音指令与App的前台交互,这篇我们讲讲如何使用Cortana调用App的后台任务,相比 ...

随机推荐

  1. 从现在开始强迫自己使用 Reflect

    静态方法 Reflect.apply(target, thisArg, args) 等同于 Function.prototype.apply.call(func, thisArg, args) Ref ...

  2. css识别空格回车符

    新闻发布系统文字需要换行,后台返回数据包含空格.回车符号需要默认显示出来.在父元素加上css样式 :white-space:pre:即可. 例:<div class="white-sp ...

  3. 命令 检查Linux服务器性能

    一.uptime命令 这个命令可以快速查看机器的负载情况.在Linux系统中,这些数据表示等待CPU资源的进程和阻塞在不可中断IO进程(进程状态为D)的数量.这些数据可以让我们对系统资源使用有一个宏观 ...

  4. Oracle单表备份三种方案

    备份方案一: 1. 备份 create table [备份名] as select * from [表名]; 2. 恢复 truncate table org_group; insert into o ...

  5. linux 用户空间与内核空间——高端内存了解

    Linux 操作系统和驱动程序运行在内核空间,应用程序运行在用户空间,两者不能简单地使用指针传递数据,因为Linux使用的虚拟内存机制,用户空间的数据可能被换出,当内核空间使用用户空间指针时,对应的数 ...

  6. 【串线篇】SpringMvc框架乱码

    提交的数据可能有乱码: * 请求乱码: *      GET请求:改server.xml:在8080端口处URIEncoding="UTF-8" *      POST请求: * ...

  7. oracle 中||

    oracle里双竖线是字符串连接运算符!

  8. Spark开发环境搭建和作业提交

    Spark高可用集群搭建 在所有节点上下载或上传spark文件,解压缩安装,建立软连接 配置所有节点spark安装目录下的spark-evn.sh文件 配置slaves 配置spark-default ...

  9. HTTP详解教程 / HTTP 响应头信息 HTTP 响应头信息

    HTTP请求头提供了关于请求,响应或者其他的发送实体的信息. 在本章节中我们将具体来介绍HTTP响应头信息.直线电机哪家好 应答头 说明 Allow 服务器支持哪些请求方法(如GET.POST等). ...

  10. paper 143:人脸验证

    持续更新ing,敬请期待! 参考:http://blog.csdn.net/stdcoutzyx/article/details/42091205  1. DeepID人脸识别算法 香港中文大学的团队 ...