c# - 如何将带有 contextMenu 的 WPF ListView 绑定(bind)到 viewModel

标签 c# wpf xaml mvvm

我正在尝试将 ListView 绑定(bind)到 List<OrderPaymentVm> OrderPayments 右键单击上下文菜单以获取 PaymentTransactionId .

我的表单 View 模型中有以下属性

  public List<OrderPaymentVm> OrderPayments
  {
     get
     {
        return _orderPayments;
     }

     private set
     {
        _orderPayments = value;
        RaisePropertyChanged(() => OrderPayments);
     }
  }

View 模型

   public class OrderPaymentVm : ViewModelBase
   {

      private RelayCommand _copyPaymentTransactionId;
      public DateTime PaymentTime { get; set; }
      public PaymentType PaymentType { get; set; }
      public string Explanation { get; set; }
      public string PaymentTransactionId { get; set; }
      public decimal Amount { get; set; }
      public RelayCommand CopyPaymentTransactionId
      {
         get { return _copyPaymentTransactionId ?? (_copyPaymentTransactionId = new RelayCommand(ExecuteCopyPaymentTransactionId)); }
      }

      private void ExecuteCopyPaymentTransactionId()
      {
         Clipboard.SetText(string.IsNullOrWhiteSpace(PaymentTransactionId) ? string.Empty : PaymentTransactionId);
      }

   }

我有以下xaml

<ListView Grid.Row="1" ItemsSource="{Binding OrderPayments}" HorizontalAlignment="Stretch" Margin="0,0,0,1">
      <ListView.ContextMenu>
         <ContextMenu>
            <MenuItem Header="Copy Transaction Id"
                  Command="{Binding CopyPaymentTransactionId}"
                  CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}" />
         </ContextMenu>
      </ListView.ContextMenu>
      <ListView.View>
         <GridView>
            <GridViewColumn HeaderContainerStyle="{StaticResource HeaderLeftAlign}" Header="Transaction Id" Width="150" DisplayMemberBinding="{Binding PaymentTransactionId}" />
            <GridViewColumn HeaderContainerStyle="{StaticResource HeaderLeftAlign}" Header="Time" Width="150" DisplayMemberBinding="{Binding PaymentTime}" />
            <GridViewColumn HeaderContainerStyle="{StaticResource HeaderLeftAlign}" Header="Payment Type" Width="100" DisplayMemberBinding="{Binding PaymentType}" />
            <GridViewColumn HeaderContainerStyle="{StaticResource HeaderLeftAlign}" Header="Amount" Width="80" DisplayMemberBinding="{Binding Amount, StringFormat='{}{0:C}'}" />
            <GridViewColumn HeaderContainerStyle="{StaticResource HeaderLeftAlign}" Header="Explanation" Width="280" DisplayMemberBinding="{Binding Explanation}" />
         </GridView>
      </ListView.View>
</ListView>

问题 1 xaml 设计器说 GridViewColumn 绑定(bind)有问题,它在它们下划线并说它无法解析属性,但是它编译并工作正常

问题 2 ConextMenu 命令未命中 viewmodel 命令 RelayCommand CopyPaymentTransactionId

我确定这些都是简单的问题,但是我正在转动我的轮子,有人有什么建议吗?

谢谢

最佳答案

这是第二个问题的解决方案。因为上下文菜单托管在弹出窗口中,它不会从其父元素继承数据上下文,因为它是一个单独的根元素。所以您不能简单地绑定(bind)到父元素的 View 模型。

这里是在上下文菜单中绑定(bind)命令的例子

Command="{Binding PlacementTarget.SelectedItem.CopyPaymentTransactionId,
                  RelativeSource={RelativeSource AncestorType=ContextMenu}}"

与命令参数类似,需要指定命令绑定(bind)的来源。

为了简化你也可以这样写

<MenuItem Header="Copy Transaction Id"
          DataContext="{Binding PlacementTarget.SelectedItem, 
                                RelativeSource={RelativeSource AncestorType=ContextMenu}}"
          Command="{Binding CopyPaymentTransactionId}"
          CommandParameter="{Binding}" />

关于c# - 如何将带有 contextMenu 的 WPF ListView 绑定(bind)到 viewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25777880/

相关文章:

c# - 不止一种控件类型的 WPF StackPanel.Resources setter ?

c# - 将所有 PDF 字段设置为只读

wpf - 如何将样式应用于自定义控件库中的 DependencyObject

c# - 如何绑定(bind) WPF listview 项目的背景颜色?

c# - 如何将 ViewModel(包括分隔符)正确绑定(bind)到 WPF 的菜单?

c# - 使用堆栈面板 - 垂直 + 水平

javascript - 使用 ASP.Net Core SDK 3.0 preview8(Blazor 框架)上传图片

c# - 尝试分配给 C# 'foreach iteration variable' 会生成编译时错误。在 Linq [IEnumerable].ForEach(...) 中这样做不会。为什么?

c# - 调用 ApplyResources 后重新应用动态添加的 UserControl 的布局

visual-studio - 添加待办事项 : into XAML so that is shows on my task list in visual studio