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();
     }

    
 }
相关推荐
聆风吟º7 小时前
CANN hccl 深度解析:异构计算集群通信库的跨节点通信与资源管控实现逻辑
人工智能·wpf·transformer·cann
无心水15 小时前
分布式定时任务与SELECT FOR UPDATE:从致命陷阱到优雅解决方案(实战案例+架构演进)
服务器·人工智能·分布式·后端·spring·架构·wpf
LZL_SQ17 小时前
HCCL测试框架中AllReduce边界条件测试设计深度剖析
wpf·cann
User_芊芊君子2 天前
【分布式训练】CANN SHMEM跨设备内存通信库:构建高效多机多卡训练的关键组件
分布式·深度学习·神经网络·wpf
就是有点傻3 天前
WPF按钮走马灯效果
wpf
zuozewei3 天前
虚拟电厂聚合商平台安全技术体系深度解读
安全·wpf
极客智造3 天前
WPF 自定义控件:AutoGrid 实现灵活自动布局的网格控件
wpf
极客智造3 天前
WPF Grid 布局高效扩展:GridHelpers 附加属性工具类全解析
wpf
张人玉3 天前
WPF 多语言实现完整笔记(.NET 4.7.2)
笔记·.net·wpf·多语言实现·多语言适配
暖馒3 天前
深度剖析串口通讯(232/485)
开发语言·c#·wpf·智能硬件