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

    
 }
相关推荐
baivfhpwxf20234 小时前
DataGrid 中增加选择列 功能实现
ui·wpf
czhc11400756637 小时前
winform 330 跨线程 异步
wpf·线程·winform
想你依然心痛8 小时前
HarmonyOS 5.0教育行业解决方案:基于分布式能力的沉浸式智慧课堂系统
分布式·wpf·harmonyos
Maybe_ch10 小时前
深度解析 WPF 线程模型:告别 UI 卡死,掌握 Dispatcher 核心机制
ui·wpf
code bean10 小时前
【Halcon 】用 Halcon 实现涂抹:Region、仿射变换与 WPF 交互
wpf·交互·halcon
白露与泡影1 天前
Spring Cloud进阶--分布式权限校验OAuth2
分布式·spring cloud·wpf
枫叶丹41 天前
【HarmonyOS 6.0】ArkData 分布式数据对象新特性:资产传输进度监听与接续传输能力深度解析
开发语言·分布式·华为·wpf·harmonyos
一念春风2 天前
智能文字识别工具(AI)
开发语言·c#·wpf
故事不长丨2 天前
WPF MvvmLight 超详细使用教程
c#·wpf·mvvm·mvvmlight
IT小哥哥呀3 天前
基于windows的个人/团队的时间管理工具
windows·c#·wpf·时间管理