简体   繁体   中英

How to get row data of each column if button is clicked inside listview

I am new to WPF. I have just bind one listview. Under my listview there is a button in each row.

When user clicks on that button, the data from that particular row should be fetched.

Below is my code

<Window x:Class="Searching.ImportedKeywords"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ImportedKeywords" Height="330.769" Width="1079.12">
    <Grid Margin="-1094,0,2,24" RenderTransformOrigin="0.621,0.497">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="0*"/>
        </Grid.ColumnDefinitions>
        <ListView Name="lvShowSearching" Height="247" VerticalAlignment="Top" RenderTransformOrigin="2.25,3.86" Margin="1100,10,0,-13">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="First Name" Width="70" DisplayMemberBinding="{Binding FirstName}" />
                    <GridViewColumn Header="Last Name" Width="70" DisplayMemberBinding="{Binding LastName}" />
                    <GridViewColumn Header="With All Of The Words" Width="100" DisplayMemberBinding="{Binding WithAllOfTheWords}" />
                    <GridViewColumn Header="With The Exact Phrase" Width="100" DisplayMemberBinding="{Binding WithTheExactPhrase}" />
                    <GridViewColumn Header="With At Least One Of These Words" Width="100" DisplayMemberBinding="{Binding WithAtLeastOneOfTheseWords}" />
                    <GridViewColumn Header="Without The Word" Width="100" DisplayMemberBinding="{Binding WithoutTheWord}" />
                    <GridViewColumn Header="Country" Width="70" DisplayMemberBinding="{Binding CountryName}"/>
                    <GridViewColumn Header="State" Width="70" DisplayMemberBinding="{Binding StateName}" />
                    <GridViewColumn Header="City" Width="70" DisplayMemberBinding="{Binding CityName}" />
                    <GridViewColumn Header="Publication" Width="70" DisplayMemberBinding="{Binding PublicationName}" />
                    <GridViewColumn Header="Total Records" Width="70" DisplayMemberBinding="{Binding TotalRecords}" />
                    <GridViewColumn Header="Total Records Imported" Width="70" DisplayMemberBinding="{Binding TotalRecordsImported}" />
                    <GridViewColumn Header="Status" Width="70" DisplayMemberBinding="{Binding Status}" />
                    <GridViewColumn Header="Import" Width="70">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button CommandParameter="{Binding}"  Click="Button_Click_1">Import</Button>
                                <!--<TextBlock Text="{Binding Mail}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />-->
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Address">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Margin="6,2,6,2">
                                    <Button Content="Address" 
                                   Command="{Binding 
                                   Path=DataContext.RunCommand, 
                                   RelativeSource=
                                   {RelativeSource FindAncestor, 
                                   AncestorType={x:Type ItemsControl}}}"/>
                                </StackPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>



 private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            IEnumerable items = this.lvShowSearching.Items;
            foreach (ClippingSearchKeyword data in items)
            {
                if (data != null)
                {

                }
            }
        }

        private void OnRunCommand(object obj)
        {
            if (obj != null)
            {

            }
        }

You can find your parent GridViewRow using below method

public static T FindVisualParent<T>(UIElement element) where T : UIElement
    {
        UIElement parent = element;
        while (parent != null)
        {
            T correctlyTyped = parent as T;
            if (correctlyTyped != null)
            {
              return correctlyTyped;
            }
            parent = VisualTreeHelper.GetParent(parent) as UIElement;
        }
        return null;
    }

And change ButtonClick event as below,You will get GridViewRow

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
      GridViewRow grvRow= FindVisualParent<GridViewRow>(sender as UIElement);

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM