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>

传递参数:

XML 复制代码
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Converter={StaticResource IndexConverter}}"

绑定索引的转换器:

cs 复制代码
 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();
     }

    
 }
相关推荐
Chris _data8 天前
WPF 学习第三天 — Modbus RTU 串口通信
hadoop·学习·wpf
布吉岛的石头8 天前
Java 程序员第 43 阶段05:微服务整合大模型,跨服务调用架构设计实战,Seata分布式事务实战
wpf
步步为营DotNet8 天前
基于.NET Aspire 实现云原生应用的高效监控与可观测性
云原生·.net·wpf
芒鸽9 天前
HarmonyOS 分布式开发实战:设备协同、数据共享与跨设备迁移
分布式·wpf·harmonyos
Volunteer Technology9 天前
Flink状态管理与容错(二)
大数据·flink·wpf
happyprince10 天前
07_verl-Trainer模块详解
人工智能·架构·wpf·强化学习
bugcome_com10 天前
WPF + Prism 技术指南与实战项目(二、模板搭建)
wpf
小满Autumn10 天前
log4net 日志框架 — 从配置到实战速查手册
笔记·c#·.net·wpf·上位机·log4net
政沅同学11 天前
基于 C# WPF + HALCON 的工业视觉算法工具框架(开源)
开发语言·c#·wpf
happyprince11 天前
03_verl-设计理念与核心原理
wpf