[英]Xamarin Forms Bind command outside ItemSource
I have a problem.我有个问题。 I created this ListView:我创建了这个 ListView:
<ListView ItemsSource="{Binding knownDeviceList}" SelectionMode="None" RowHeight="90" ItemTapped="device_Clicked">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Command="{Binding DeleteDevice}"
CommandParameter="{Binding Id}"
Text="Delete" IsDestructive="True" />
</ViewCell.ContextActions>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The ListView is bound to a List with objects in it, but I need to bind the command to an ICommand outside the List on root level of the ViewModel. ListView 绑定到一个包含对象的 List,但我需要将命令绑定到 ViewModel 根级别的 List 之外的 ICommand。 How can I do that, because now the ICommand doesn't get triggered when I try to remove an item from the List!我该怎么做,因为现在当我尝试从列表中删除项目时不会触发 ICommand!
Here is my Command in my ViewModel:这是我的 ViewModel 中的命令:
public ICommand DeleteDevice
{
get
{
return new Command<int>((x) => RemoveDevice_Handler(x));
}
}
What am I doing wrong?我究竟做错了什么?
Your MenuItem.BindingContext
is scoped to the actual item in that cell, not the view model of the whole page (or ListView
).您的MenuItem.BindingContext
范围限于该单元格中的实际项目,而不是整个页面(或ListView
)的视图模型。 You will either need to tell the binding that it needs to looks else where, like this:您要么需要告诉绑定它需要查看其他位置,如下所示:
<ListView x:Name="MyListView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Command="{Binding Path=BindingContext.DeleteDevice, Source={x:Reference MyListView}}}"/>
</ViewCell.ContextActions>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Note that I removed the attributes that you have in there just to make clear which ones I added.请注意,我删除了您在那里拥有的属性,只是为了明确我添加了哪些属性。 You can keep them in, this is just for readability.您可以保留它们,这只是为了便于阅读。
Or, you can use the new Relative Bindings .或者,您可以使用新的相对绑定。 Then you would implement the command binding like this:然后你可以像这样实现命令绑定:
Command="{Binding Source={RelativeSource AncestorType={x:Type local:YourViewModelClass}}, Path=DeleteDevice}"
The Context
that you are trying to bind with from the Command
is not the Page
's one but the ItemSource
, This is why you can simply bind the Id
to the CommandParameter
.您尝试从Command
绑定的Context
不是Page
的,而是ItemSource
,这就是您可以简单地将Id
绑定到CommandParameter
。
To fix this, you need to target the page's BindingContext
since your Command
lives at the ViewModel root level.要解决此问题,您需要定位页面的BindingContext
因为您的Command
位于 ViewModel 根级别。 You can achieve it by adding a x:Name
property to your ListView
and target the right Context
through it.您可以通过向ListView
添加x:Name
属性并通过它定位正确的Context
来实现它。
Here the fix:这里的修复:
<ListView x:Name="listView" ItemsSource="{Binding knownDeviceList}" SelectionMode="None" RowHeight="90" ItemTapped="device_Clicked">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Command = "{Binding BindingContext.DeleteDevice, Source={x:Reference listView}}"
CommandParameter="{Binding Id}"
Text="Delete" IsDestructive="True" />
</ViewCell.ContextActions>
</ViewCell>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
Hope it helps & happy coding!希望它有帮助和快乐编码!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.