简体   繁体   中英

UWP C# ListView return the Id value of the clicked row

I am fairly new to coding and I am building a database UI.

What I need to do is click on a row in a listview and get the return of the Id .

Below is the code I have but it gets an 'Exception User-Unhandled System.ArgumentNullException: value cannot be null' .

Any help will be predicated.

UWP Xaml <ListView x:Name="ListItems" IsItemClickEnabled="True" ItemClick="ListItems_ItemClick" Tag="{Binding Id}"
                             >

private void ListItems_ItemClick(object sender, ItemClickEventArgs e)
    {

        var id = (sender as ListView).Tag as string;
        {
            testbox.Text = id;
        }

Full List View

<ListView x:Name="ListItems" IsItemClickEnabled="True" ItemClick="ListItems_ItemClick" Tag="{Binding Id}"
                             >
                        <ListView.ItemTemplate >
                            <DataTemplate >
                                <Border
                         BorderThickness="0,0,0,0">
                                    <Grid HorizontalAlignment="Stretch">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="250" />
                                            <ColumnDefinition Width="130" />
                                            <ColumnDefinition Width="150" />
                                            <ColumnDefinition Width="200" />
                                            <ColumnDefinition Width="200" />
                                            <ColumnDefinition Width="200" />
                                            <ColumnDefinition Width="auto" />
                                        </Grid.ColumnDefinitions>
                                        <Grid.Resources>
                                            <Style TargetType="TextBlock">
                                                <Setter Property="Padding" Value="8,0,0,0" />
                                            </Style>
                                        </Grid.Resources>
                                        <TextBlock
                                            Grid.Column="0"
                                            HorizontalAlignment="Stretch"
                                            Text="{Binding LegalName, Mode=OneWay}" />
                                        <TextBlock
                                            Grid.Column="1"
                                            HorizontalAlignment="Stretch"
                                            Text="{Binding PhoneNumber, Mode=OneWay}" />
                                        <TextBlock
                                            Grid.Column="2"
                                            HorizontalAlignment="Stretch"
                                            Text="{Binding EmailAddress, Mode=OneWay}" />
                                        <TextBlock
                                            Grid.Column="3"
                                            HorizontalAlignment="Stretch"
                                            Text="{Binding HomeAddress, Mode=OneWay}" />
                                        <TextBlock
                                            Grid.Column="4"
                                            HorizontalAlignment="Stretch"
                                            Text="{Binding PostalAddress, Mode=OneWay}" />
                                        <TextBlock
                                            Grid.Column="5"
                                            HorizontalAlignment="Stretch"
                                            Text="{Binding Id, Mode=OneWay}" />
                                    </Grid>
                                </Border>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

Here's a very easy approach. Bind the TextChanged event of all the texboxes to a TextChanged event :

XAML

 <TextBlock Grid.Column="0" TextChanged="textbox_TextChanged".../>

C#

 private string selectedText;
 private void textbox_TextChanged(object sender, EventArgs e)
 {
  selectedtext = (sender as TextBlock).Text
 }

 private void ListItems_ItemClick(object sender, ItemClickEventArgs e)
{
 ///use selectedtext string as you want
}

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