XAML的ListView ItemTapped/ItemSelected命令绑定
本文关键字:ItemSelected 命令 绑定 ItemTapped ListView XAML | 更新日期: 2023-09-27 18:13:13
我如何从我的ViewModel(目前在BindingContext)绑定到ItemTapped或ItemSelected从一个ListView在XAML的iccommand对象?
这是一个简单的任务,当使用一个按钮,我只是设置Command="MyViewModelCommand"和一切工作
我不知道为什么当你可以直接在ViewModel中获得选中/选定对象时,你需要在页面中给出Item Selected或Item Tapped事件:
我想你已经用下面的代码绑定了listview
<ListView ItemsSource="{Binding PermitDetails}"
SelectedItem="{Binding objItemSelected, Mode=TwoWay}" x:Name="lst"
RowHeight="35" HorizontalOptions="FillAndExpand"
VerticalOptions="Fill">
在与页面绑定的视图模型中,您必须定义属性objItemSelected
,如下面的代码
private Permit _ItemSelected;
public Permit objItemSelected {
get {
return _ItemSelected;
}
set {
if (_ItemSelected != value) {
_ItemSelected = value;
OnPropertyChanged ("ItemSelected");
}
}
}
如果你想执行任何额外的功能,比如导航到详细页面,你可以在OnPropertyChanged语句执行后从set属性中完成。
希望这对你有帮助!
这是一个老问题,也许这是最近的发展,但您可以使用任何控件上的行为来将事件转换为iccommand。
一个例子……
<ListView ItemsSource="{Binding Tags}"
SelectedItem="{Binding SelectedTag}">
<ListView.Behaviors>
<behaviors:EventHandlerBehavior EventName="ItemSelected">
<behaviors:InvokeCommandAction Command="{Binding SelectedTagChanged}" />
</behaviors:EventHandlerBehavior>
</ListView.Behaviors>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ContentView Padding="8">
<Label Text="{Binding DisplayValue}" />
</ContentView>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在本例中,ItemSelected事件将映射到视图模型中的SelectedTagChanged命令,如下所示…
public Command SelectedTagChanged
{
get
{
return new Command(row =>
{
// do something
});
}
}
我的场景是一个键盘,图像是按钮,当单击一个按钮时,相关字符会显示在标签中。
.
假设您有以下控件(键1):
<Image x:Name="imgKey1"
Source="key1.png"
Aspect="AspectFit" />
并且你希望当图像被点击时触发来自"viewModel"对象的命令。我的"viewModel"有以下属性:
public ICommand AddCharCommand { protected set; get; }
在我的类(视图模型)构造函数我有:
this.AddCharCommand = new Command<string>((key) =>
{
// Add the key to the input string.
this.InputString += key;
});
"InputString"是一个字符串属性…
对于绑定视图和模式,我做了如下操作:
imgKey1.GestureRecognizers.Add(new TapGestureRecognizer
{
Command = viewModel.AddCharCommand,
CommandParameter = "1",
});
我希望我能帮助你,如果有人需要什么,我可以帮助;我在这里……。JJ <Image Source="n1normal.png"
Aspect="AspectFit" >
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding AddCharCommand}"
CommandParameter ="1"/>
</Image.GestureRecognizers>
</Image>
//-OR-
<Image Source="magnifyglass.png"
Aspect="AspectFit"
HorizontalOptions="End">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="Search"/>
</Image.GestureRecognizers>
</Image>
//And for the Search create in your ViewModel
private void Search(object sender, EventArgs e)
{
//Your code here
}
我过去采用将所选项绑定到视图模型的方法,但这往往会在过滤编程更改和用户操作时产生麻烦。我个人最喜欢的是附加属性
public static readonly BindableProperty ListItemTappedCommandProperty = BindableProperty.CreateAttached<AttachedProperties, ICommand>(
staticgetter: o => (ICommand) o.GetValue(ListItemTappedCommandProperty),
defaultValue: default(ICommand),
propertyChanged: (o, old, @new) =>
{
var lv = o as ListView;
if (lv == null) return;
lv.ItemTapped -= ListView_ItemTapped;
lv.ItemTapped += ListView_ItemTapped;
});
private static void ListView_ItemTapped(object sender, object item)
{
var lv = sender as ListView;
var command = (ICommand) lv?.GetValue(ListItemTappedCommandProperty);
if (command == null) return;
if (command.CanExecute(item))
command.Execute(item);
}
…然后在ListView上设置:
controls:AttachedProperties.ListItemTappedCommand="{Binding ItemSelectedCommand}"
这甚至可以扩展为使用事件名称并更普遍地应用,但这是另一次练习。
以下是使用Xamarin社区工具包的最新答案
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
>
<ListView .... >
<ListView.Behaviors>
<xct:EventToCommandBehavior
EventName="ItemSelected"
Command="{Binding MyCustomCommand}" />
</ListView.Behaviors>
</ListView >
裁判https://learn.microsoft.com/en-us/xamarin/community-toolkit/behaviors/eventtocommandbehavior
为什么不将listview的selectedItem属性绑定到viewModel属性(让我们说一个'selectedItem'属性);然后在viewModel中的'selectedItem'属性的setter上执行命令。