如何从listview对象发送者xamarin中获取所选项目
问题描述:
嗨
使用ListView行为,如何获取所选项目或所选行的值/对象?
下面是我尝试的,我得到一个说SelectedItem的属性,但我无法访问它。我错过了什么?
我的尝试:
Hi
Using ListView Behaviors, How do I get values/object of selected item or selected row?
Below is what i tried and I get a property that says "SelectedItem" but i cant access it. What am I missing?
What I have tried:
<ListView.Behaviors>
<behaviors:EventHandlerBehavior EventName="ItemSelected">
<behaviors:InvokeCommandAction Command="{Binding OnItemSelected}" />
</behaviors:EventHandlerBehavior>
</ListView.Behaviors>
//constructor
public BurialsViewModel()
{
_onItemSelected = new Command(OnSelectedItem);
}
// viewmodel
//viewmodel
ICommand _onItemSelected;
public ICommand OnItemSelected
{
get { return _onItemSelected; }
}
void OnSelectedItem(object sender)
{
//I need values from sender
}
答
很难从有限的代码片段中分辨出来。您似乎没有将ListView
控件的ItemsSource
绑定到中的集合视图模型
。此外,应该有一个SelectedItem
属性,您可以绑定到该属性而不按照您的方式执行。 [更新:]以下是如何数据绑定: ListView数据源 - Xamarin [ ^ ]
Xamarin [ ^ ]有一个 tonne您可以学习的资源 [ ^ ]。这是一个提供超出您需要的链接,但将涵盖您想要的内容: Multi - 选择ListView - Xamarin [ ^ ]。另请查看他们的论坛...... [ ^ ]
Hard to tell from the limited code snippets. It appears that you have not bound theItemsSource
of theListView
control to the collection in yourViewModel
. Also, there should be aSelectedItem
property that you can bind to without doing it the way that you are. [update:] Here is how to databind: ListView Data Sources - Xamarin[^]
Xamarin[^] has a tonne of resources[^] that you can learn from. Here is a link that offers more than what you need but will cover what you want: Multi-select ListView - Xamarin[^]. Also check out their forums...[^]