如何将数据(ListView的selectItem)从xaml发送到ViewModel [英] How send data (selectItem of listView) from xaml to ViewModel
问题描述
我想在我的viewModel中选择数据
I want get data selected in my viewModel
这是我的xaml,但是我不知道如何解决我的xaml,因为这很不好,如何在我的xaml中使用我的行为?
this is my xaml, but i donot know how solved my xaml, because this is bad, how use my behavior here in my xaml?
<ListView.Behaviors>
<behaviors:ItemTappedBehavior EventName="ItemSelected">
<behaviors:InvokeCommandAction Command="{Binding SelectedTagChanged}" />
</behaviors:ItemTappedBehavior>
</ListView.Behaviors>
<ListView ItemsSource="{Binding actors}" ItemTapped="ListView_ItemTapped">
在我的邮政编码
public partial class ActorsView : ContentPage
{
public AccountsView()
{
InitializeComponent();
}
async void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
Actor selectedItem = (Actor)e.Item;
Console.WriteLine("WORK"+ Actor.Name);
}
但是我想进入我的viewModel,不要进入我的背后代码
but i want to get in my viewModel, dont in my behind code
我见过要解决的是命令或行为
i've seen to solved is with commands, or behaviors
这是我的viewModel:
this is my viewModel:
public class ActorsViewModel : ViewModelBase
{
public List<Actor> actors { get; set; }
public AccountsViewModel(IActorManager actorManager)
: base()
{
编辑,我正在使用命令,但我不知道如何使用John Livermore的答案,我想使用并显示控制台Console.WriteLine("ROW");.
Edit, i am using commands but i dont know how use the answer from John Livermore, i want to use and show the console Console.WriteLine("ROW");.
public class ItemTappedBehavior : Behavior<ListView>
{
public ICommand Command { get; set; }
protected override void OnAttachedTo(ListView bindable)
{
base.OnAttachedTo(bindable);
}
protected override void OnDetachingFrom(ListView bindable)
{
base.OnDetachingFrom(bindable);
}
public Command SelectedTagChanged
{
get
{
return new Command(row =>
{
Console.WriteLine("ROW");
});
}
}
}
推荐答案
由于您要求的ListView选择行为更加简单.请使用以下内容:
Since you had asked for a simpler Behavior for ListView selection changed. Please use the following:
Following class is inherited from BehaviorBase
which takes care of BindingContext allocation.
- 在
OnAttachedTo
覆盖上,钩住了ItemSelected事件. - 在
OnDetachingFrom
覆盖上,未解除ItemSelected事件的锁定. -
Command
可绑定属性绑定到ViewModel
中的Command
- 在
e.SelectedItem
传递给命令的情况下. (改变 它等于您希望传递的任何值)
- On the
OnAttachedTo
override the ItemSelected event is hooked. - On the
OnDetachingFrom
override the ItemSelected event is unhooked. Command
bindable property Binds to the Command inViewModel
- In the event the
e.SelectedItem
is passed to the Command. (Change it to whatever value you wish to pass)
public class SelectionChangedEventBehavior : BehaviorBase<ListView>
{
public static readonly BindableProperty CommandProperty = BindableProperty.Create("Command", typeof(ICommand), typeof(SelectionChangedEventBehavior), null);
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
protected override void OnAttachedTo(BindableObject bindable)
{
base.OnAttachedTo(bindable);
AssociatedObject.ItemSelected += AssociatedObject_ItemSelected;
}
protected override void OnDetachingFrom(ListView bindable)
{
base.OnDetachingFrom(bindable);
AssociatedObject.ItemSelected -= AssociatedObject_ItemSelected;
}
private void AssociatedObject_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (Command != null)
{
Command.Execute(e.SelectedItem);
}
}
}
用法
Xaml
<ListView ItemsSource="{Binding Collection}">
<ListView.Behaviors>
<local:SelectionChangedEventBehavior Command="{Binding SelectionCommand}" />
</ListView.Behaviors>
</ListView>
ViewModel
ViewModel
public class ViewModel : INotifyPropertyChanged
{
public Command<object> SelectionCommand { get; set; }
public ViewModel()
{
SelectionCommand = new Command<object>(OnSelection);
}
private void OnSelection(object item)
{
var selectedActor = (item as Actor);
}
}
希望这对您可能有用.如果可能,请使用 EventToCommandBehavior ,因为您可以将其重用于任何控件和任何事件.
Hope this could be useful for you. If possible use the EventToCommandBehavior as whole, just because you can reuse it for any control and any event.
这篇关于如何将数据(ListView的selectItem)从xaml发送到ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!