当前位置:网站首页>【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
边栏推荐
- Lock lock
- Node access to Alipay open platform sandbox to achieve payment function
- Loggie source code analysis source file module backbone analysis
- Encapsulating the logging module
- SQL database
- PostgreSQL column storage and row storage
- 杂文 谈谈古典的《拆掉思维里的墙》
- Your brain expands and shrinks over time — these charts show how
- Lock锁
- Variable length parameter__ VA_ ARGS__ Macro definitions for and logging
猜你喜欢
Shell script -- shell programming specification and variables
Document operation II (5000 word summary)
Use case execution of robot framework
On lambda powertools typescript
Loggie source code analysis source file module backbone analysis
Milvus 2.0 质量保障系统详解
Node access to Alipay open platform sandbox to achieve payment function
Detailed explanation of Milvus 2.0 quality assurance system
PyTorch:train模式与eval模式的那些坑
Nacos + aspnetcore + Ocelot actual combat code
随机推荐
Error in v-on handler: "typeerror: cannot read property 'resetfields' of undefined"
Lock锁
True math problems in 1959 college entrance examination
Linux MySQL data timing dump
ByteVCharts可视化图表库,你想要的我都有
Public variables of robotframework
Grpc gateway based on Ocelot
Dancenn: overview of byte self-developed 100 billion scale file metadata storage system
PyTorch:train模式与eval模式的那些坑
Your brain expands and shrinks over time — these charts show how
Go language, array, string, slice
深入了解3D模型相关知识(建模、材质贴图、UV、法线),置换贴图、凹凸贴图与法线贴图的区别
Mock test using postman
DDT + Excel for interface test
org. apache. parquet. schema. InvalidSchemaException: A group type can not be empty. Parquet does not su
◰GL-着色器处理程序封装
VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
Change the password after installing MySQL in Linux
MySQL modify master database
Use between nodejs modules