news 2026/9/7 0:14:07

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/9/2 20:16:03

2.4 Docker核心概念详解:镜像、容器、仓库三分钟彻底搞懂

2.4 Docker核心概念详解:镜像、容器、仓库三分钟彻底搞懂 引言 理解Docker的核心概念是掌握Docker的关键。镜像(Image)、容器(Container)和仓库(Repository)是Docker的三个核心概念。本文将用通俗易懂的方式解释这三个概念及其关系。 一、镜像(Image) 1.1 什么是镜…

作者头像 李华
网站建设 2026/9/2 22:11:53

2.7 Docker镜像安全扫描:Trivy和Clair实战检测镜像漏洞

2.7 Docker镜像安全扫描:Trivy和Clair实战检测镜像漏洞 引言 镜像安全是容器安全的重要环节。通过安全扫描工具检测镜像中的漏洞,可以及时发现和修复安全问题。本文将介绍Trivy和Clair两个主流的安全扫描工具,帮助你构建安全的容器镜像。 一、镜像安全概述 1.1 安全威胁…

作者头像 李华
网站建设 2026/9/6 21:17:25

YOLOv12官版镜像效果展示:小目标也能看清

YOLOv12官版镜像效果展示&#xff1a;小目标也能看清 你有没有遇到过这样的场景&#xff1a;监控画面里一只飞鸟只有十几个像素&#xff0c;工业质检中电路板上的微小焊点 barely 可见&#xff0c;无人机巡检时远处的电力塔螺栓模糊成点——传统YOLO模型在这些场景下要么漏检&…

作者头像 李华
网站建设 2026/9/2 23:11:53

GPEN输出文件在哪?新手最容易忽略的细节解答

GPEN输出文件在哪&#xff1f;新手最容易忽略的细节解答 你刚跑完GPEN人像修复脚本&#xff0c;终端显示“Done”&#xff0c;图片也生成成功了——可翻遍整个镜像目录&#xff0c;就是找不到那个叫output_Solvay_conference_1927.png的文件&#xff1f;别急&#xff0c;这不是…

作者头像 李华
网站建设 2026/9/3 1:51:16

从上传图片到结果输出:完整体验cv_resnet18_ocr-detection全流程

从上传图片到结果输出&#xff1a;完整体验cv_resnet18_ocr-detection全流程 1. 这不是一个“调参工程师”的OCR工具&#xff0c;而是一个能立刻上手的检测服务 你有没有过这样的经历&#xff1a; 刚拿到一张扫描件、一张商品详情截图、或者一张证件照片&#xff0c;想快速提…

作者头像 李华
网站建设 2026/9/2 9:48:49

网络工程师和网络安全工程师哪个更有前景?

我们经常接到私信&#xff0c;问网络工程师和网络安全工程师的区别是什么&#xff1f; 网络工程师是负责设计、实施和维护计算机网络系统的专业人员。 他们处理网络架构、硬件和软件配置、网络安全、性能优化等方面的工作。网络工程师的主要任务包括网络规划、网络设备配置、网…

作者头像 李华