C# 如何将ItemSelected事件从可重用的Listview绑定到ViewModal命令?
我有一个UserList,我想在Xamarin应用程序的多个页面上重用它,但是在每个不同的页面上,ItemSelected事件应该做一些不同的事情 我知道可绑定属性。我使用它们将Viewmodel中的列表绑定到可重用组件。但我不知道如何处理这些事件 让我给你看一些代码 这是我的可重用ListView的XAML。它包含一个用户列表C# 如何将ItemSelected事件从可重用的Listview绑定到ViewModal命令?,c#,listview,xamarin.forms,data-binding,C#,Listview,Xamarin.forms,Data Binding,我有一个UserList,我想在Xamarin应用程序的多个页面上重用它,但是在每个不同的页面上,ItemSelected事件应该做一些不同的事情 我知道可绑定属性。我使用它们将Viewmodel中的列表绑定到可重用组件。但我不知道如何处理这些事件 让我给你看一些代码 这是我的可重用ListView的XAML。它包含一个用户列表 <?xml version="1.0" encoding="UTF-8"?> <ContentView xmlns="http://xamarin.c
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="KineAppForms.Views.ReusableViews.UserTable"
x:Name="this">
<ContentView.Content>
<ListView ItemsSource="{Binding Source={x:Reference this},Path=Users, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name.GivenName}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentView.Content>
</ContentView>
进行绑定的.cs文件如下所示
namespace KineAppForms.Views.ReusableViews
{
public partial class UserTable : ContentView
{
public ObservableCollection<Patient> Users
{
get
{
return (ObservableCollection<Patient>)GetValue(UsersProperty);
}
set
{
SetValue(UsersProperty, value);
}
}
public static readonly BindableProperty UsersProperty =
BindableProperty.Create("Users", typeof(ObservableCollection<Patient>), typeof(UserTable), null,
BindingMode.Default, null, OnItemsSourceChanged);
static void OnItemsSourceChanged(BindableObject bindable, object oldvalue, object newvalue)
{
System.Diagnostics.Debug.WriteLine("source changed");
}
public UserTable()
{
InitializeComponent();
}
}
}
命名空间KineAppForms.Views.ReusableViews
{
公共部分类UserTable:ContentView
{
公共可观测收集用户
{
收到
{
return(observeCollection)GetValue(UsersProperty);
}
设置
{
SetValue(UsersProperty,value);
}
}
公共静态只读BindableProperty UsersProperty=
Create(“用户”、typeof(observeCollection)、typeof(UserTable)、null、,
BindingMode.Default,null,OnItemSourceChanged);
静态void OnItemSourceChanged(BindableObject bindable、object oldvalue、object newvalue)
{
System.Diagnostics.Debug.WriteLine(“源代码已更改”);
}
公共用户表()
{
初始化组件();
}
}
}
现在,我如何使用我的可重用组件
<views:UserTable Users="{Binding PatientList ,Mode=TwoWay}" />
绑定PatientList来自ViewModel
否结束问题:
如何将ItemSelected事件绑定到ViewModel中的命令
假设我有两页。1页为患者,1页为医生。它们都使用相同的表,但“患者”表应转到患者的详细页面(链接到PatientListModel中的goToPatientDetailCommand),而“医生”表应转到医生的详细页面(链接到DoctorListModel中的goToPatientDetailCommand)
应该是这样的
<views:UserTable Users="{Binding PatientList ,Mode=TwoWay}" OnItemSelected="{Binding GoToPatientDetailed, Mode=TwoWay }/>
谢谢大家! 这里有几种方法
ItemSelectedCommand
添加到UserTable类public部分类UserTable:ContentView
{
公共可观测收集用户
{
收到
{
return(observeCollection)GetValue(UsersProperty);
}
设置
{
SetValue(UsersProperty,value);
}
}
公共静态只读BindableProperty UsersProperty=
Create(“用户”、typeof(observeCollection)、typeof(UserTable)、null、,
BindingMode.Default,null,OnItemSourceChanged);
公共静态BindableProperty项SelectedCommandProperty=BindableProperty.Create(
propertyName:nameof(ItemSelectedCommand),
returnType:typeof(ICommand),
declaringType:typeof(用户表),
默认值:空);
公共ICommand ItemSelectedCommand
{
获取{return(ICommand)GetValue(ItemSelectedCommandProperty);}
set{SetValue(ItemSelectedCommandProperty,value);}
}
静态void OnItemSourceChanged(BindableObject bindable、object oldvalue、object newvalue)
{
System.Diagnostics.Debug.WriteLine(“源代码已更改”);
}
公共用户表()
{
初始化组件();
}
}
然后在Xaml中,您可以通过添加
<ListView.Behaviors>
<behaviors:EventToCommandBehavior EventName="ItemSelected" Command="{Binding Source={x:Reference this},Path=ItemSelectedCommand}"/>
</ListView.Behaviors>
或者,您可以创建自己的自定义ListView
(从ListView继承的),该视图中的ItemSelected如下所示
ListView
创建控件。你有关于如何做的文档李>
如果您征求我的意见,我会说使用第二种方法-稍后您可以在其他地方重用该视图。您可以使用来使用事件触发命令。由于您使用的是两个不同的viewmodels,所以应该可以正常工作。嗨!谢谢你的这篇文章,它真的很有帮助!在本例中,是否可以将EventArguments传递给viewmodel。因此,如果单击一个用户,UserModel将被传递给ViewModel命令。哪个示例?使用行为还是自定义列表视图?两者都有可能,但我需要知道你喜欢哪一个;-)你好对于行为,请:)
public partial class UserTable : ContentView
{
public ObservableCollection<Patient> Users
{
get
{
return (ObservableCollection<Patient>)GetValue(UsersProperty);
}
set
{
SetValue(UsersProperty, value);
}
}
public static readonly BindableProperty UsersProperty =
BindableProperty.Create("Users", typeof(ObservableCollection<Patient>), typeof(UserTable), null,
BindingMode.Default, null, OnItemsSourceChanged);
public static BindableProperty ItemSelectedCommandProperty = BindableProperty.Create(
propertyName: nameof(ItemSelectedCommand),
returnType: typeof(ICommand),
declaringType: typeof(UserTable),
defaultValue: null);
public ICommand ItemSelectedCommand
{
get { return (ICommand)GetValue(ItemSelectedCommandProperty); }
set { SetValue(ItemSelectedCommandProperty, value); }
}
static void OnItemsSourceChanged(BindableObject bindable, object oldvalue, object newvalue)
{
System.Diagnostics.Debug.WriteLine("source changed");
}
public UserTable()
{
InitializeComponent();
}
}
<ListView.Behaviors>
<behaviors:EventToCommandBehavior EventName="ItemSelected" Command="{Binding Source={x:Reference this},Path=ItemSelectedCommand}"/>
</ListView.Behaviors>