简体   繁体   中英

How to extract certain data cell from Listview / Gridview wpf

Hi please keep in mind I am new with WPF , I am using a WebService that returns a gridlist[] and it automatically populate my Gridview . here is the WebService :

C#:

WebService.Contacts Contact = new WebService.Contacts();
grdGetGroup.ItemsSource = Contact.GetGroups(Username, Password);

Here is my XAML :

<ListView x:Name="grdGetGroup"
          Margin="560,34,128,48"
          FontSize="13"
          BorderBrush="#FFF01F1F"
          Foreground="#FFF01F1F"
          SelectedIndex="1"
          FontFamily="/WPF Working Experimenet;component/Font/#B Nazanin">
    <ListView.View>
        <GridView>
            <GridViewColumn x:Name="GridID"
                            Header="ID"
                            Width="50"
                            DisplayMemberBinding="{Binding GroupID}"
                            FrameworkElement.FlowDirection="RightToLeft" />
            <GridViewColumn Header="Group Name"
                            Width="85
                                                            "
                            DisplayMemberBinding="{Binding GroupName}"
                            FrameworkElement.FlowDirection="RightToLeft" />
            <GridViewColumn Header="Numbers"
                            Width="60"
                            DisplayMemberBinding="{Binding ContactCount}"
                            FrameworkElement.FlowDirection="RightToLeft" />
            <GridViewColumn Header="Access"
                            Width="60"
                            DisplayMemberBinding="{Binding ShowToChild}"
                            FrameworkElement.FlowDirection="RightToLeft" />
            <GridViewColumn Header="Description"
                            Width="150"
                            DisplayMemberBinding="{Binding GroupDescription}"
                            FrameworkElement.FlowDirection="RightToLeft" />
            <GridViewColumn Header=""
                            Width="60"
                            FrameworkElement.FlowDirection="RightToLeft">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <Button x:Name="btnChangeGroup"
                                    Margin="5"
                                    Content="Change"
                                    Cursor="Hand"
                                    Click="btnChangeGroup_Click">
                                <Button.Template>
                                    <ControlTemplate TargetType="Button">
                                        <TextBlock TextDecorations="Underline">
                                                                            <ContentPresenter /></TextBlock>
                                    </ControlTemplate>
                                </Button.Template>
                                <Button.Style>
                                    <Style TargetType="Button">
                                        <Setter Property="Foreground"
                                                Value="Black" />
                                        <Style.Triggers>
                                            <Trigger Property="IsMouseOver"
                                                     Value="true">
                                                <Setter Property="Foreground"
                                                        Value="Red" />
                                            </Trigger>
                                        </Style.Triggers>
                                    </Style>
                                </Button.Style>
                            </Button>
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header=""
                            Width="60"
                            FrameworkElement.FlowDirection="RightToLeft">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <Button x:Name="btnRemoveGroup"
                                    Margin="5"
                                    Content="Remove"
                                    Cursor="Hand"
                                    Click="btnRemoveGroup_Click">
                                <Button.Template>
                                    <ControlTemplate TargetType="Button">
                                        <TextBlock TextDecorations="Underline">
                                                                            <ContentPresenter /></TextBlock>
                                    </ControlTemplate>
                                </Button.Template>
                                <Button.Style>
                                    <Style TargetType="Button">
                                        <Setter Property="Foreground"
                                                Value="Black" />
                                        <Style.Triggers>
                                            <Trigger Property="IsMouseOver"
                                                     Value="true">
                                                <Setter Property="Foreground"
                                                        Value="#FFF01F1F" />
                                            </Trigger>
                                        </Style.Triggers>
                                    </Style>
                                </Button.Style>
                            </Button>
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

If you have noticed, I have 2 button for every row in Gridview which is built automatically as well, now here is my question how to extract the Data in GridviewColumn x:Name"GridID" ? and pass it to the same row's Button Click Event (Which has a method that takes the Data has an input)

Valkyry,

If I undestand/guess right, on every line you ve got a gridList item.

If I undestand/guess right each gridList has a GroupID property.
Correct me if am wrong.

In WPF, in a DataGrid, each line has a DataContext property that holds the data of the line :

void btnRemoveGroup_Click( Object sender, EventArgs args)
{
    var fxElt = sender as FrameworkElement;
    var lineData = fxElt.DataContext as gridlist;
    int groupID = lineData .GroupID;
}

Tell me it works or not. Regards

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