当前位置:网站首页>【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
边栏推荐
- Decimal format decimal / datetime conversion processing
- How to implement distributed locks with redis?
- Knowledge points and examples of [seven input / output systems]
- Variable length parameter__ VA_ ARGS__ Macro definitions for and logging
- How vscode compares the similarities and differences between two files
- Redis docker installation
- English | day15, 16 x sentence true research daily sentence (clause disconnection, modification)
- Introduction to new functions of camtasia2022 software
- Detailed explanation of Niuke - Gloves
- 蓝桥杯省一之路06——第十二届省赛真题第二场
猜你喜欢

信息摘要、数字签名、数字证书、对称加密与非对称加密详解

Document operation II (5000 word summary)

The new MySQL table has a self increasing ID of 20 bits. The reason is
![Knowledge points and examples of [seven input / output systems]](/img/5f/759a73836f79ef848f449930dcc1b1.png)
Knowledge points and examples of [seven input / output systems]

Milvus 2.0 质量保障系统详解

Dancenn: overview of byte self-developed 100 billion scale file metadata storage system

VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN

Get the column name list of the table quickly in Oracle

Path environment variable

How much do you know about the process of the interview
随机推荐
Zhimeng dedecms security setup Guide
◰GL-着色器处理程序封装
Multithreaded @ async thread pool
Collect blog posts
Tencent resolves the address according to the IP address
博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
批量制造测试数据的思路,附源码
Getting started with JDBC
The new MySQL table has a self increasing ID of 20 bits. The reason is
Read a blog, re understand closures and tidy up
Pycham connects to the remote server and realizes remote debugging
Derivation of Σ GL perspective projection matrix
PHP efficiently reads large files and processes data
Nifi fast installation and file synchronization
How does flash cache data in memory?
Rtklib 2.4.3 source code Notes
Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
面试百分百问到的进程,你究竟了解多少
[registration] tf54: engineer growth map and excellent R & D organization building
5-minute NLP: text to text transfer transformer (T5) unified text to text task model