[英]How to Get Id when click on Grid item in ListView In UWP
I am developing a Universal windows app, 我正在开发通用Windows应用程序,
I am consuming service to get list of the companies details, and i am binding those list of companies to list view. 我正在使用服务来获取公司详细信息列表,并且将那些公司列表绑定到列表视图。
But now when click on each company i want id of that that company to call another service based on that company id. 但是现在,当单击每个公司时,我希望该公司的ID可以基于该公司ID调用另一个服务。
following is my xaml view 以下是我的xaml视图
<ListView BorderBrush="#DCDCDC" Background="#DCDCDC" Grid.Row="1" x:Name="listBoxobj" HorizontalAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Border BorderThickness="1">
<StackPanel Orientation="Vertical">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding name}" TextWrapping="Wrap" FontSize="15" FontWeight="Bold" />
</Grid>
<StackPanel BorderBrush="White" Background="White">
<TextBlock Text="Current Status" TextWrapping="Wrap" FontSize="15"/>
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Row="0" Grid.Column="0" BorderBrush="#DCDCDC" BorderThickness="1" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<Image x:Name="tone" Tapped="tone_Tapped" Source="../Icons/tower-signal-interface-symbol.png" Height="40" HorizontalAlignment="Center"></Image>
</StackPanel>
<StackPanel Grid.Row="1">
<TextBlock Text="Tone Alert" HorizontalAlignment="Center" Grid.Row="0" Grid.Column="0" TextWrapping="Wrap" FontSize="15"/>
</StackPanel>
</Grid>
</Grid>
</StackPanel>
</StackPanel>
</Border>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
Thanks, Srinivas. 谢谢,斯里尼瓦斯。
ListView has SelectedItem property - bind it to your ViewModel as SelectedCompany. ListView具有SelectedItem属性-将其作为SelectedCompany绑定到您的ViewModel。 Then, when it changes, call you API with SelectedCompany.Id.
然后,当其更改时,使用SelectedCompany.Id调用您的API。
You can do it in a few ways. 您可以通过几种方式来实现。
In SelectedCompany setter launch async request. 在SelectedCompany setter中启动异步请求。
Use ReactiveUI and in your VM constructor: 在您的VM构造函数中使用ReactiveUI :
this.WhenAnyValue(x => x.SelectedCompany).Where(x => x != null).Select(x => x.Id).InvokeCommand(GetCompanyDetails);
It will use SelectedCompany Id as command parameter. 它将使用SelectedCompany Id作为命令参数。
EDIT: 编辑:
After you showed your code, I need to change my answer. 显示代码后,我需要更改答案。 In your case you can just put your image inside a button a bind command with parameter:
在您的情况下,您可以将图像放在按钮内,然后将其绑定为带有参数的命令:
<Button Grid.Row="0" Command="{Binding ViewModel.GetCompanyDetails}" CommandParameter={Binding Id}">
<Image x:Name="tone" Tapped="tone_Tapped" Source="../Icons/tower-signal-interface-symbol.png" Height="40" HorizontalAlignment="Center"></Image>
</Button>
In XAML, when you use ListView and DataTemplate, each item of ItemsSource is set as DataContext for DataTemplate in each row. 在XAML中,当您使用ListView和DataTemplate时,ItemsSource的每个项目都将设置为每行中DataTemplate的DataContext。
The other way is to set Tag for button and retrieve it in your click event handler. 另一种方法是为按钮设置Tag并在您的click事件处理程序中检索它。
XAML: <Button Grid.Row="0" Click="GetCompanyDetails" Tag={Binding Id}"> <Image x:Name="tone" Tapped="tone_Tapped" Source="../Icons/tower-signal-interface-symbol.png" Height="40" HorizontalAlignment="Center"></Image> </Button>
XAML:
<Button Grid.Row="0" Click="GetCompanyDetails" Tag={Binding Id}"> <Image x:Name="tone" Tapped="tone_Tapped" Source="../Icons/tower-signal-interface-symbol.png" Height="40" HorizontalAlignment="Center"></Image> </Button>
and in code-behind: 并在代码背后:
public void GetCompanyDetails(object sender, RoutedEventArgs e)
{
var id = (sender as Button).Tag as int;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.