当前位置:网站首页>【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
边栏推荐
- Error in v-on handler: "typeerror: cannot read property 'resetfields' of undefined"
- RTKLIB 2.4.3源码笔记
- Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption
- Milvus 2.0 质量保障系统详解
- Camtasia2022软件新增功能介绍
- Paging SQL
- Introduction to new functions of camtasia2022 software
- Use case execution of robot framework
- How to choose the wireless gooseneck anchor microphone and handheld microphone scheme
- 【解决报错】Error in v-on handler: “TypeError: Cannot read property ‘resetFields’ of undefined”
猜你喜欢
自定义my_strcpy与库strcpy【模拟实现字符串相关函数】
STM32__03—初识定时器
Do you really understand the principle of code scanning login?
Solution architect's small bag - 5 types of architecture diagrams
Modify the test case name generated by DDT
oracle 中快速获取表的列名列表
【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验
groutine
∑GL-透视投影矩阵的推导
Derivation of Σ GL perspective projection matrix
随机推荐
PHP高效读大文件处理数据
groutine
Regular filtering of Intranet addresses and segments
自定义my_strcpy与库strcpy【模拟实现字符串相关函数】
_ Mold_ Board_
Idea of batch manufacturing test data, with source code
Log4j output log information to file
MySQL master-slave synchronization pit avoidance version tutorial
Milvus 2.0 質量保障系統詳解
NVIDIA graphics card driver error
Redis docker installation
STM32__ 03 - beginner timer
Pseudo Distributed installation spark
How does flash cache data in memory?
BUG_ me
Modify the test case name generated by DDT
Tencent resolves the address according to the IP address
Use case labeling mechanism of robot framework
Get the column name list of the table quickly in Oracle
How vscode compares the similarities and differences between two files