当前位置:网站首页>【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
边栏推荐
- Path environment variable
- Quick install mongodb
- STM32__ 03 - beginner timer
- _ Mold_ Board_
- Get the column name list of the table quickly in Oracle
- Website_ Collection
- Derivation of Σ GL perspective projection matrix
- An essay on the classical "tear down the wall in thinking"
- Zhongang Mining: Fluorite Flotation Process
- 5-minute NLP: text to text transfer transformer (T5) unified text to text task model
猜你喜欢

SQL database

Path environment variable

【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验

ByteVCharts可视化图表库,你想要的我都有

Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption

Bytevcharts visual chart library, I have everything you want

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

Installation and management procedures

Quick install mongodb

Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
随机推荐
Loggie source code analysis source file module backbone analysis
Mock test
ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
Milvus 2.0 质量保障系统详解
AIOT产业技术全景结构-数字化架构设计(8)
Dancenn: overview of byte self-developed 100 billion scale file metadata storage system
Path environment variable
Shortcut keys (multiline)
File upload and download of robot framework
扫码登录的原理你真的了解吗?
Nifi fast installation and file synchronization
[PROJECT] small hat takeout (8)
Log4j output log information to file
Public variables of robotframework
Shell脚本——Shell编程规范及变量
Kingdee Cloud Star API calling practice
Calculation formula related to tolerance analysis
计算饼状图百分比
Go language RPC communication
websocket