【WPF】ListView数据绑定

1.ListView数据绑定

csharp 复制代码
 <ListView
     ItemsSource="{Binding List}"
     SelectionChanged="ListView_SelectionChanged"
     ScrollViewer.VerticalScrollBarVisibility="Auto"
     SelectedItem="{Binding Model}">

     <!--  ListView设置列内容居中  -->
     <ListView.ItemContainerStyle>
         <Style TargetType="ListViewItem">
             <Setter Property="HorizontalContentAlignment" Value="Stretch" />
             <Setter Property="Height" Value="30" />
         </Style>
     </ListView.ItemContainerStyle>

     <!--  ListView中的列  -->
     <ListView.View>
         <GridView AllowsColumnReorder="True">
             <GridViewColumn
                 Width="200"
                 DisplayMemberBinding="{Binding No}"
                 Header="字段1" />
             <GridViewColumn
                 Width="180"
                 DisplayMemberBinding="{Binding UpdateTime}"
                 Header="字段2" />
             <GridViewColumn Width="80" Header="{DynamicResource archives_Operate}">
                 <GridViewColumn.CellTemplate>
                     <DataTemplate>
                         <Button
                             Command="{Binding Path=DataContext.ConfigCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"
                             CommandParameter="{Binding}"
                             Content="操作"
                             Style="{DynamicResource LinkButton}" />
                     </DataTemplate>
                 </GridViewColumn.CellTemplate>
             </GridViewColumn>
         </GridView>
     </ListView.View>
 </ListView>

2.注意事项

SelectionChanged选择事件,清空数据需要重新new数据集,否则点击选择会有问题

相关推荐
hqwest19 小时前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局
hqwest1 天前
C#WPF实战出真汁08--【消费开单】--餐桌面板展示
c#·wpf·ui设计·wpf界面设计
orangapple1 天前
WPF 打印报告图片大小的自适应(含完整示例与详解)
c#·wpf
三千道应用题2 天前
WPF&C#超市管理系统(6)订单详情、顾客注册、商品销售排行查询和库存提示、LiveChat报表
开发语言·c#·wpf
✎ ﹏梦醒͜ღ҉繁华落℘3 天前
开发WPF项目时遇到的问题总结
wpf
hqwest4 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
Vae_Mars4 天前
WPF中使用InputBindings进行快捷键绑定
wpf
hqwest4 天前
C#WPF实战出真汁05--左侧导航
开发语言·c#·wpf·主界面·窗体设计·视图viewmodel
hqwest4 天前
C#WPF实战出真汁01--项目介绍
开发语言·c#·wpf
wuty0075 天前
WPF 实现支持动态调整高度的文本显示控件
wpf·scrollviewer·extentheight·自动高度控件·动态调整高度