当前位置: 首页 > news >正文

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(); } }
http://www.jsqmd.com/news/302712/

相关文章:

  • 零基础搞定AI抠图!用科哥的UNet镜像快速处理人像透明化
  • 2026年10个免费交通工具音效素材网站推荐!
  • 优化开题报告撰写流程:介绍9款AI写作辅助工具及模板修改详细指南
  • 告别繁琐配置!用FSMN VAD镜像快速实现电话录音分析
  • 学术写作效率提升方案:9款人工智能工具与开题报告模板修改技巧解析
  • YOLOv12官版镜像效果展示:小目标也能看清
  • 提升研发效率:国内主流智能专利网站一览,个人专利代办/企业专利代理/专利申请代办/专利申请,智能专利工具哪家好
  • 2026年适合中小企业的GEO优化服务商推荐:按预算分档选择指南
  • 2026 年中小企业 GEO 优化服务商实测:哪家性价比最高且效果可验证?
  • Sivers 赢得 80 万美元美国国防合同
  • 语音情绪识别太神奇!科哥镜像让我5分钟就上手
  • 快速理解verl:Single-controller模式详解
  • 基于AI预测模型与数据洞察:3月按兵不动?美联储或将延至6月开启降息
  • PyTorch-2.x镜像结合Matplotlib画出惊艳损失曲线
  • 零基础学AI:用Qwen3-1.7B快速实现文本生成任务
  • ⚡_延迟优化实战:从毫秒到微秒的性能突破[20260126040549]
  • 零代码门槛!图形化操作也能微调大模型?亲测可行
  • 网络安全工程师,网络安全入门到精通,看这一篇就够了!
  • 【2026年最新整理】网络安全工程师需要具备的8个重要技能
  • 网络工程师和网络安全工程师哪个更有前景?
  • 【网络安全工程师】什么是网络安全工程师,你想知道的都在这里!
  • 分析国内菌菇企业哪个更值得选,为你筛选优质品牌
  • 2026年靠谱的老式麻辣烫加盟专业公司,糊涂婶口碑良好
  • [特殊字符]_Web框架性能终极对决:谁才是真正的速度王者[20260126035705]
  • 领新机械费用多少,考察产品竞争力与耐用性值得投资吗
  • 手把手教你写开机启动脚本,让Armbian设备一开机就亮灯
  • Qwen-Image-2512-ComfyUI使用全记录:小白也能变高手
  • Z-Image-Turbo优化指南:显存占用还能再降?
  • Z-Image-Turbo_UI界面实战:输入提示词秒出高清图
  • YOLOE镜像SAVPE机制解析,视觉提示更精准