【问题标题】:Xamarin Forms Bind command outside ItemSourceXamarin Forms Bind 命令在 ItemSource 之外
【发布时间】:2020-03-17 05:02:29
【问题描述】:

我有问题。我创建了这个 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>

ListView 绑定到其中包含对象的 List,但我需要将命令绑定到 ViewModel 根级别的 List 之外的 ICommand。我该怎么做,因为现在我尝试从列表中删除项目时不会触发 ICommand!

这是我的 ViewModel 中的命令:

public ICommand DeleteDevice
{
    get
    {
        return new Command<int>((x) => RemoveDevice_Handler(x));
    }
}

我做错了什么?

【问题讨论】:

    标签: c# xamarin xamarin.forms xamarin.android xamarin.ios


    【解决方案1】:

    您的MenuItem.BindingContext 的范围仅限于该单元格中的实际项目,而不是整个页面的视图模型(或ListView)。你要么需要告诉绑定它需要在其他地方寻找,就像这样:

    <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>
    

    请注意,我删除了您在其中的属性只是为了清楚我添加了哪些属性。您可以保留它们,这只是为了便于阅读。

    或者,您可以使用新的Relative Bindings。然后你会像这样实现命令绑定:

    Command="{Binding Source={RelativeSource AncestorType={x:Type local:YourViewModelClass}}, Path=DeleteDevice}"

    【讨论】:

      【解决方案2】:

      您尝试从Command 绑定的Context 不是Page,而是ItemSource,这就是为什么您可以简单地将Id 绑定到CommandParameter .

      要解决此问题,您需要定位页面的 BindingContext,因为您的 Command 位于 ViewModel 根级别。您可以通过将x:Name 属性添加到您的ListView 并通过它定位正确的Context 来实现它。

      这里是修复:

      <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>
      

      希望它对您有所帮助并祝您编码愉快!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-20
        • 2019-12-29
        • 2018-03-17
        • 2019-03-30
        • 2021-08-03
        • 1970-01-01
        • 2020-02-12
        • 2019-11-07
        相关资源
        最近更新 更多