简体   繁体   English

ItemSource 外部的 Xamarin 表单绑定命令

[英]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.

相关问题 Xamarin.Forms (PRISM) - 将复选框绑定到 viewmodel 中的命令,在 listview itemsource 之外 - Xamarin.Forms (PRISM) - Bind checkbox, to a command in viewmodel, outside listview itemsource 如何在ItemSource外部绑定DataGridComboBoxColumn - How to bind a DataGridComboBoxColumn outside the ItemSource 如何创建ItemSource Xamarin的命令 - How to create a command of ItemSource Xamarin Xamarin Forms - 调用事件“ItemSelected”时使自定义单元格绑定到原始列表视图项目源 - Xamarin Forms - Make custom cell bind to original listview itemsource when calling event "ItemSelected" Xamarin Forms使用Prism在自定义控件中绑定命令 - Xamarin Forms bind a command in a custom controler with Prism Xamarin Forms,使用async来应用ListView ItemSource - Xamarin Forms, using async to apply ListView ItemSource Xamarin Forms iOS中的嵌套Bindabale布局ItemSource - Nested Bindabale Layout ItemSource in Xamarin Forms iOS 将条目文本绑定到Xamarin.Forms中的按钮命令的CanExecute方法 - Bind Entry TextChanged to the CanExecute method of button command in Xamarin.Forms MVVM Xamarin Forms - 绑定视图模型属性的命令参数 - MVVM Xamarin Forms - Command Pararameter to Bind View Model property WPF UserControl - UserControl DataGrid的SelectedItem,用于将ItemSource绑定到UserControl外部的DataGrid - WPF UserControl - SelectedItem of a Usercontrol DataGrid to bind to a ItemSource to DataGrid outside the UserControl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM