如何在按钮上获取ListView行的索引单击

时间:2016-11-25 00:10:42

标签: c# wpf

我正在制作一个wpf应用程序并遇到问题。 我使用了listview并通过Xaml添加了一个Button。

 <ListView x:Name="list_View" Grid.Column="2" Margin="0,0,0,53" Grid.Row="1" MouseDoubleClick="list_View_MouseDoubleClick" ItemContainerStyle="{DynamicResource alternatingListViewItemStyle}" AlternationCount="2">
        <ListView.Style>
            <Style TargetType="{x:Type ListView}">
                <Setter Property="BorderBrush" Value="Blue"/>
                <Setter Property="BorderThickness" Value="2"/>
                <Setter Property="Margin" Value="0"/>
            </Style>
        </ListView.Style>
        <ListView.Resources>
            <Style x:Key="alternatingListViewItemStyle" TargetType="{x:Type ListViewItem}">
                <!-- setting row height here -->
                <Setter Property="Height" Value="30" />
                <Setter Property="BorderBrush" Value="Blue"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="Background" Value="#E7F8FE"/>

            </Style>

        </ListView.Resources>

        <ListView.View>

            <GridView>
                <GridViewColumn Header="Product Name"  Width="100"  DisplayMemberBinding="{Binding Product_Name}"/>
                <GridViewColumn Header="Unit Price" Width="70" DisplayMemberBinding="{Binding Unit_Price}"/>
                <GridViewColumn Header="Quantity" Width="50" DisplayMemberBinding="{Binding Quantity}"/>
                <GridViewColumn Header="Total"  Width="60" DisplayMemberBinding="{Binding Total}"  />
                <GridViewColumn Header="Delete"  Width="60">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="X" Width="30" Click="Delete_Item_In_Cart_Click" Name="btn_Delete_Item_In_Cart">

                            </Button>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

            </GridView>
        </ListView.View>
    </ListView>

Lisview我看起来像这样: ListView Image

我想在Button Click上获取Listview Row的索引。 按钮单击后面的代码是:

    private void Delete_Item_In_Cart_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            int index2 = list_View.SelectedIndex;
            MessageBox.Show(index2.ToString());

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }  

    } 

**问题:**当我点击listview的任何一行中的Button时,它总是给我相同的索引, 我得到的输出是: this Message Box

先谢谢

2 个答案:

答案 0 :(得分:2)

您当前的程序正在返回所选行的索引。单击按钮,将不会选中该行。要获得所需的结果,请选择按钮上的行。

以下是选择按钮点击行的代码。

代码背后:

private void DeleteListItem(object sender, RoutedEventArgs e)
{
  var curItem = ((ListBoxItem)listView.ContainerFromElement((Button)sender));
  listView.SelectedItem = (ListBoxItem)curItem;
  MessageBox.Show($"Selected index = {listView.SelectedIndex}");

  // Add code for delete item here
}

XAML - 列出视图示例代码:

<ListView x:Name="listView" HorizontalAlignment="Left" Height="301" Margin="10,10,0,0" VerticalAlignment="Top" Width="400">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="ID"  Width="100" DisplayMemberBinding="{Binding ID}"/>
            <GridViewColumn Header="Name"  Width="190" DisplayMemberBinding="{Binding Name}"/>
            <GridViewColumn Header="Delete"  Width="100">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="X" Width="30" Click="DeleteListItem"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
    <ListViewItem>
        <local:User ID="1" Name="John" />
    </ListViewItem>
    <ListViewItem>
        <local:User ID="2" Name="Michael" />
    </ListViewItem>
    <ListViewItem>
        <local:User ID="3" Name="Smith" />
    </ListViewItem>
    <ListViewItem>
        <local:User ID="4" Name="Mary" />
    </ListViewItem>
</ListView>

答案 1 :(得分:2)

最后我解决了问题。以下是我所遵循的解决方案和流程。

1:首先,我将这些行添加到<ListView.Resourses/>

中现有的Xaml中
          <Style TargetType="ListViewItem">
                <Style.Triggers>
                    <Trigger Property="IsKeyboardFocusWithin" Value="True">
                        <Setter Property="IsSelected" Value="True"/>

                    </Trigger>
                </Style.Triggers>
                <EventSetter Event="PreviewGotKeyboardFocus" Handler="SelectCurrentItem"/>
            </Style>

2:对于C#代码,我在Xaml中添加了上面提到的SelectCurrentItem处理程序。

以下是为我执行实际任务的事件处理程序代码。

    private void SelectCurrentItem(object sender, KeyboardFocusChangedEventArgs e)
    {                                                                            
        //By this Code I got my `ListView` row Selected.
        ListViewItem item = (ListViewItem)sender;
        item.IsSelected = true;

    }

3:现在获取所选ListView行的索引对我来说非常简单易行。 Button Click背后的旧代码完美地为我工作。

所以最后我在这里给出了经过全面测试且没有错误的所有代码。

1: Xaml: 这是ListView

的代码
 <ListView x:Name="list_View" Grid.Column="2" Grid.Row="1" Margin="0,0,0,103">

        <ListView.Style>
            <Style TargetType="{x:Type ListView}">

                <Setter Property="BorderBrush" Value="Blue"/>
                <Setter Property="BorderThickness" Value="2"/>
                <Setter Property="Margin" Value="0"/>
            </Style>
        </ListView.Style>
        <ListView.Resources>
            <Style TargetType="ListViewItem">
                <Style.Triggers>
                    <Trigger Property="IsKeyboardFocusWithin" Value="True">
                        <Setter Property="IsSelected" Value="True"/>

                    </Trigger>
                </Style.Triggers>
                <EventSetter Event="PreviewGotKeyboardFocus" Handler="SelectCurrentItem"/>
                <Setter Property="Height" Value="30" />
                <Setter Property="BorderBrush" Value="Blue"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="Background" Value="#E7F8FE"/>
            </Style>

        </ListView.Resources>

        <ListView.View>

            <GridView>
                <GridViewColumn Header="Product Name"  Width="120"  DisplayMemberBinding="{Binding Product_Name}"/>
                <GridViewColumn Header="Unit Price" Width="70" DisplayMemberBinding="{Binding Unit_Price}"/>
                <GridViewColumn Header="Quantity" Width="50" DisplayMemberBinding="{Binding Quantity}"/>
                <GridViewColumn Header="Total"  Width="60" DisplayMemberBinding="{Binding Total}"  />
                <GridViewColumn Header="Delete"  Width="80">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="X" Width="60" Click="Delete_Item_In_Cart_Click" Name="btn_Delete_Item_In_Cart" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

                            </Button>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

            </GridView>
        </ListView.View>

    </ListView>

2: C#代码:

    private void SelectCurrentItem(object sender, KeyboardFocusChangedEventArgs e)
    {
        ListViewItem item = (ListViewItem)sender;
        item.IsSelected = true;

    }

private void Delete_Item_In_Cart_Click(object sender, RoutedEventArgs e)
{
    try
    {   
        int index =0;
        index = list_View.SelectedIndex;
        MessageBox.Show(index.ToString());

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }  

}