gpt4 book ai didi

c# - UWP C# ListView 返回被点击行的Id值

转载 作者:太空宇宙 更新时间:2023-11-03 14:54:36 25 4
gpt4 key购买 nike

我是编码新手,正在构建数据库 UI。

我需要做的是单击 listview 中的一行并获取 Id 的返回值。

下面是我的代码,但它得到了一个'Exception User-Unhandled System.ArgumentNullException: value cannot be null'

任何帮助都会被断言。

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;
}

完整 ListView

<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>

最佳答案

这是一个非常简单的方法。将所有文本框的 TextChanged 事件绑定(bind)到一个 TextChanged 事件:

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
}

关于c# - UWP C# ListView 返回被点击行的Id值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50196081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com