当前位置:网站首页>【WPF绑定3】 ListView基础绑定和数据模板绑定
【WPF绑定3】 ListView基础绑定和数据模板绑定
2022-04-23 17:02:00 【code bean】
介绍了ListBox的基础绑定,今天记录下ListView的相关绑定。
如果说ListBox是一个单列表,那么ListView就是一个带列标题的多列表。
如:
这个列表有个特点,第一列和第二列都是字符串,第三列是一个控件TextBox。
所以今天会用到两种绑定,基础的绑定和模板绑定(DataTemplate)
基础绑定和ListBox类似,ListBox用到了DisplayMemberPath属性
<ListBox Name="list_axis" DisplayMemberPath="Name" SelectedValuePath="Num" SelectionChanged="list_axis_SelectionChanged"/>
ListView中有个属性叫 DisplayMemberBinding,因不止一列,所以需要进行套娃,像这样:
<ListView x:Name="lv_pos" ItemContainerStyle="{StaticResource ListViewItemStyle}">
<ListView.View>
<GridView>
<GridViewColumn Header="名字" Width="auto" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="别名" Width="auto" DisplayMemberBinding="{Binding ShowName}"/>
</GridView>
</ListView.View>
</ListView>
第三列,我用的是Textbox控件,这样就支持编辑,所以需要用到模板绑定,像这样:
<ListView x:Name="lv_pos" ItemContainerStyle="{StaticResource ListViewItemStyle}">
<ListView.View>
<GridView>
<GridViewColumn Header="名字" Width="auto" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="别名" Width="auto" DisplayMemberBinding="{Binding ShowName}"/>
<GridViewColumn Header="位置" Width="120">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Width="auto" MinWidth="80" Text="{Binding Path=Pos, Mode=TwoWay}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
最后来看数据源部分:
先自定义一个类:
public class AxisPosInfo
{
public AxisPosInfo(string name, string show_name, double pos)
{
Name = name;
ShowName = show_name;
Pos = pos;
}
public AxisPosInfo()
{
}
public string Name { get; set; }
public string ShowName { get; set; }
public double Pos { get; set; }
}
在定义一个数组:
public List<AxisPosInfo> PosList { get; set; }
最后利用ListView的ItemsSource 和数据源进行绑定。
lv_pos.ItemsSource = axis_info.PosList;
版权声明
本文为[code bean]所创,转载请带上原文链接,感谢
https://blog.csdn.net/songhuangong123/article/details/124360207
边栏推荐
- Deepinv20 installation MariaDB
- Detailed explanation of Niuke - Gloves
- JSON deserialize anonymous array / object
- 【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验
- Go language RPC communication
- Nacos + aspnetcore + Ocelot actual combat code
- oracle 中快速获取表的列名列表
- Pytorch: the pit between train mode and eval mode
- How to choose the wireless gooseneck anchor microphone and handheld microphone scheme
- 扫码登录的原理你真的了解吗?
猜你喜欢
vscode如何比较两个文件的异同
Getting started with JDBC
【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验
Derivation of Σ GL perspective projection matrix
MySQL master-slave synchronization pit avoidance version tutorial
org. apache. parquet. schema. InvalidSchemaException: A group type can not be empty. Parquet does not su
Go language, array, string, slice
Get the column name list of the table quickly in Oracle
The new MySQL table has a self increasing ID of 20 bits. The reason is
Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
随机推荐
Deepinv20 installation MariaDB
Pycham connects to the remote server and realizes remote debugging
Redis docker installation
Milvus 2.0 质量保障系统详解
文件操作《二》(5000字总结篇)
Generation of barcode and QR code
SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
VsCode-Go
Go language, array, string, slice
DDT + Excel for interface test
How to build tiktok user trust and drive fan growth
Aiot industrial technology panoramic structure - Digital Architecture Design (8)
VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
Installing labellmg tutorial in Windows
1-4 configuration executable script of nodejs installation
◰GL-着色器处理程序封装
【题解】[SHOI2012] 随机树
Smart doc + Torna generate interface document
Detailed explanation of Milvus 2.0 quality assurance system
[pimf] openharmony paper Club - what is the experience of wandering in ACM survey