作为命令参数传入 WPF ListView GridViewColumn 的值

Passing in as command parameter the value of a WPF ListView GridViewColumn

在 WPF ListView 中,我有 ListViewItem 由三列组成,第一列是自动递增列,第二列是 TextBox,最后一列包含同一列中的两个按钮(编辑和删除)。

我的 ListView 现在看起来如下:

<ListView x:Name="MyListView"
          AlternationCount="{Binding pathList.Count}"
          ItemsSource="{Binding pathList}" 
          IsSynchronizedWithCurrentItem="True">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="#" x:Name="LvItemId"
                            DisplayMemberBinding="{Binding (ItemsControl.AlternationIndex),
                            RelativeSource={RelativeSource AncestorType=ListViewItem}}"/>
            <GridViewColumn Header="Command sentence" Width="350" CellTemplate="{StaticResource NameColumnTemplate}"/>
            <GridViewColumn Width="70" Header="Edit / Delete" CellTemplate="{StaticResource AddDelItemTemplate}"/>
        </GridView>
    </ListView.View>
</ListView>

CellTemplate 下方:

<DataTemplate x:Key="FirstRowTemplate1">
    <Grid Width="190">
        <Button Content="Add" Command="{Binding Path=DataContext.AddCommand, ElementName=MyListView}" 
                Width="180" 
                HorizontalAlignment="Center"/>
    </Grid>
</DataTemplate>
<DataTemplate x:Key="NextRowTemplate1">
    <TextBox Text="{Binding PathString}"/>
</DataTemplate>
<DataTemplate x:Key="NameColumnTemplate">
    <ContentPresenter x:Name="ButtonPresenter"
                      Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"
                      ContentTemplate="{StaticResource NextRowTemplate1}"/>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Add}" Value="True">
            <Setter TargetName="ButtonPresenter"  
                    Property="ContentTemplate" 
                    Value="{StaticResource FirstRowTemplate1}"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Add}" Value="False">
            <Setter TargetName="ButtonPresenter"
                    Property="ContentTemplate"
                    Value="{StaticResource NextRowTemplate1}"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>
<DataTemplate x:Key="FirstRowTemplate2"/>
<DataTemplate x:Key="NextRowTemplate2">
    <StackPanel Orientation="Horizontal">
        <Button Content="Edit" Command="{Binding Path=DataContext.EditCommand, ElementName=MyListView}" CommandParameter="{Binding ElementName=LvItemId, Path=WhatToPutHere}"/>
        <Button Content="Delete" Command="{Binding Path=DataContext.DelCommand, ElementName=MyListView}" CommandParameter="{Binding ElementName=LvItemId, Path=WhatToPutHere}"/>
    </StackPanel>
</DataTemplate>
<DataTemplate x:Key="AddDelItemTemplate">
    <ContentPresenter x:Name="ButtonPresenter"
            Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"  
            ContentTemplate="{StaticResource NextRowTemplate2}"/>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Add}" Value="True">
            <Setter TargetName="ButtonPresenter"
                    Property="ContentTemplate" 
                    Value="{StaticResource FirstRowTemplate2}"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Add}" Value="False">
            <Setter TargetName="ButtonPresenter"
                    Property="ContentTemplate"
                    Value="{StaticResource NextRowTemplate2}"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

单击“编辑”或“删除”按钮时,我想将第一个 GridViewColumn 的值作为命令参数传递给 ListViewItem。我该怎么做?

例如:

<Button Content="Edit" Command="{Binding Path=DataContext.EditCommand, ElementName=MyListView}" CommandParameter="{Binding ElementName=LvItemId, Path=WhatToPutHere}"/>
<Button Content="Delete" Command="{Binding Path=DataContext.DelCommand, ElementName=MyListView}" CommandParameter="{Binding ElementName=LvItemId, Path=WhatToPutHere}"/>

GridViewColumn 定义了列本身,它是如何填充的,例如模板、绑定或宽度,但它没有对项目容器生成器生成的任何项目的引用。

如果是第一列,您可以复制附件的绑定 属性 AlternationIndex:

<Button Content="Edit" Command="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType=ListViewItem}}"/>
<Button Content="Delete" Command="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType=ListViewItem}}"/>