news 2026/8/29 10:34:55

WPF ListBoxItem绑定自己在ListBox中的顺序

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
WPF ListBoxItem绑定自己在ListBox中的顺序

案例,一个ListBox绑定后台实体链表:

界面显示三个模块,自定义模板实现:

顺序一般来说在C#的链表里从0开始,我这里想让其从1开始,使用了这种方法,可以传递顺序到后台命令。

<ListBox ItemContainerStyle="{StaticResource ActionItemStyle}" ItemsSource="{Binding OverallActions}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> <ListBox.ItemTemplate> <DataTemplate> <Border Margin="3" BorderThickness="1" CornerRadius="5"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <!-- 显示配置信息 --> <StackPanel Grid.Column="0" Margin="10"> <TextBlock FontSize="14" FontWeight="Bold" Text="{Binding ActionDescription}" /> </StackPanel> <!-- 索引显示 --> <TextBlock Grid.Column="1" Margin="10" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="16" FontWeight="Bold" Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Converter={StaticResource IndexConverter}}" /> <!-- 操作按钮 --> <StackPanel Grid.Column="2" Margin="10" Orientation="Horizontal"> <!-- 主操作按钮 --> <Button Width="60" Height="30" Margin="0,0,5,0" Command="{Binding DataContext.ExecuteActionHandleModelCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Converter={StaticResource IndexConverter}}" Content="执行" /> </StackPanel> </Grid> </Border> </DataTemplate> </ListBox.ItemTemplate> </ListBox>

传递参数:

CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Converter={StaticResource IndexConverter}}"

绑定索引的转换器:

public class ItemToIndexConverter : IValueConverter { // 单值转换(用于ListBoxItem/DataGridRow获取索引) public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is ListBoxItem listBoxItem) { var listBox = ItemsControl.ItemsControlFromItemContainer(listBoxItem); if (listBox != null) { int index = listBox.ItemContainerGenerator.IndexFromContainer(listBoxItem); return index >= 0 ? (index + 1).ToString() : "N/A"; } } else if (value is DataGridRow dataGridRow) { var dataGrid = ItemsControl.ItemsControlFromItemContainer(dataGridRow); if (dataGrid != null) { int index = dataGrid.ItemContainerGenerator.IndexFromContainer(dataGridRow); return index >= 0 ? (index + 1).ToString() : "N/A"; } } return "N/A"; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/21 17:51:48

跨平台虚拟机网络故障排查全景指南:从物理层到应用层的深度解析

虚拟机网络连接问题堪称IT运维的"日常绊脚石"&#xff0c;无论是开发环境配置、测试场景搭建还是生产系统部署&#xff0c;Linux/Windows宿主机与VMware虚拟机间的网络互通故障都会直接影响工作效率。本文将构建一套系统化的故障排查方法论&#xff0c;通过28个典型场…

作者头像 李华
网站建设 2026/8/26 3:09:17

跨平台虚拟机网络故障排查全景指南:从物理链路到协议层的深度解析

虚拟机网络故障是开发者和运维人员最常遇到的技术难题之一&#xff0c;尤其当Linux/Windows混合宿主机环境遇上VMware复杂的虚拟网络架构时&#xff0c;问题排查往往如同在迷宫中寻找出口。本文将系统梳理从物理层到应用层的全栈排查方法论&#xff0c;通过30实战案例、12个核心…

作者头像 李华
网站建设 2026/8/21 17:51:43

如何借助AI工具快速完成开题报告:精选9款写作软件及模板优化方法

工具对比速览 工具名称 核心功能 适用场景 效率评分 特色优势 AIBiYe 开题报告生成/降重 中文论文全流程 ★★★★★ 国内院校适配度高 AICheck 初稿生成/格式检查 快速产出框架 ★★★★☆ 结构化输出优秀 AskPaper 文献综述辅助 外文文献处理 ★★★★ 跨…

作者头像 李华
网站建设 2026/8/28 11:31:53

一键部署YOLO11,让计算机视觉更简单

一键部署YOLO11&#xff0c;让计算机视觉更简单 你是否曾为配置YOLO环境反复折腾数小时&#xff1f;装错CUDA版本、pip依赖冲突、路径报错、GPU识别失败……这些不是学习目标&#xff0c;而是本不该存在的障碍。YOLO11作为Ultralytics最新发布的高效视觉模型系列&#xff0c;在…

作者头像 李华
网站建设 2026/8/21 17:52:00

语音情绪识别太神奇!科哥镜像让我5分钟就上手

语音情绪识别太神奇&#xff01;科哥镜像让我5分钟就上手 你有没有试过&#xff0c;听一段语音就能立刻判断说话人是开心、生气&#xff0c;还是紧张不安&#xff1f;不是靠经验猜测&#xff0c;而是用AI精准识别——现在&#xff0c;这已经不是科幻场景了。上周我拿到科哥打包…

作者头像 李华
网站建设 2026/8/29 9:00:48

中文语音识别怎么选?Seaco Paraformer实测推荐

中文语音识别怎么选&#xff1f;Seaco Paraformer实测推荐 在中文语音识别&#xff08;ASR&#xff09;领域&#xff0c;模型选择常让人纠结&#xff1a;是追求高精度还是低延迟&#xff1f;要不要支持热词定制&#xff1f;部署是否简单&#xff1f;能否兼顾专业术语和日常口语…

作者头像 李华