news 2026/4/2 17:37:23

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/3/14 9:54:39

小白也能用!Qwen-Image-2512-ComfyUI保姆级修图实战教程

小白也能用&#xff01;Qwen-Image-2512-ComfyUI保姆级修图实战教程 你是不是也遇到过这些情况&#xff1a; 朋友发来一张合影&#xff0c;想把背景里乱入的路人P掉&#xff0c;但PS抠图半天边缘还是毛毛的&#xff1b;做小红书封面&#xff0c;文字加了阴影、描边、渐变&…

作者头像 李华
网站建设 2026/3/19 6:56:29

人工智能助力学术写作:9款高效完成开题报告的实用工具与模板技巧

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

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

开箱即用:cv_resnet18_ocr-detection五分钟跑通流程

开箱即用&#xff1a;cv_resnet18_ocr-detection五分钟跑通流程 1. 为什么这个OCR检测模型值得你花五分钟试试&#xff1f; 你有没有遇到过这样的场景&#xff1a;手头有一张商品说明书截图&#xff0c;想快速提取其中的参数表格&#xff1b;或者收到一份扫描版合同&#xff…

作者头像 李华
网站建设 2026/3/28 7:25:59

终于找到合适的本地绘图方案!麦橘超然使用心得分享

终于找到合适的本地绘图方案&#xff01;麦橘超然使用心得分享 1. 为什么我一直在找这个工具&#xff1f; 去年开始折腾本地 AI 绘图&#xff0c;试过七八个方案&#xff1a;ComfyUI 配置像解谜、Automatic1111 插件堆到崩溃、Stable Diffusion WebUI 在 RTX 3060 上跑一张图…

作者头像 李华
网站建设 2026/3/30 22:54:23

于磊老师Geo 优化:Schema.org 与 JSON-LD 的战略价值

在生成式人工智能&#xff08;Generative AI&#xff09;主导信息检索与分发的时代&#xff0c;传统的搜索引擎优化&#xff08;SEO&#xff09;正加速向生成式引擎优化&#xff08;Geo&#xff09;演进。Geo 的核心在于如何高效、准确地将内容实体嵌入 AI 的知识图谱&#xff…

作者头像 李华
网站建设 2026/3/20 14:03:52

Qwen-Image-2512-ComfyUI使用全记录:小白也能变高手

Qwen-Image-2512-ComfyUI使用全记录&#xff1a;小白也能变高手 1. 这不是另一个“点几下就能出图”的教程 你可能已经试过好几个图片生成工具&#xff0c;打开网页、输几句话、等几十秒、下载结果——然后发现&#xff1a;颜色不对、手长了三只、文字糊成一团、或者干脆没生…

作者头像 李华