简体   繁体   English

列表框项目上下文菜单WPF和MVVM

[英]Listbox item Context Menu WPF and MVVM

I have tried a lot of solutions but non of them work for me. 我尝试了很多解决方案,但没有一个对我有用。 I am using WPF and MVVM. 我正在使用WPF和MVVM。

I have the following XAML file 我有以下XAML文件

<UserControl x:Class="MyApp.View.AvailableItems"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MyApp.View"
             xmlns:presenter="clr-namespace:MyApp.ViewModel.Presenter"
             mc:Ignorable="d" 
             d:DataContext="{d:DesignInstance presenter:MainPresenter}"
             d:DesignHeight="300" d:DesignWidth="300">

    <DockPanel Margin="10">
        <Label DockPanel.Dock="Top">Available Items</Label>
        <ListBox ItemsSource="{Binding ItemsList}" SelectedItem="{Binding SelectedListItem}">
            <ListBox.ContextMenu>
                <ContextMenu DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Self}}">
                    <MenuItem Header="Delete" Command="{Binding Path=DataContext.RemoveSelectedItem}" 
                              CommandParameter="{Binding SelectedListItem}" />
                </ContextMenu>
            </ListBox.ContextMenu>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"
                                   FontWeight="Bold" Foreground="Blue"/>

                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </DockPanel>
</UserControl>

And I have my MainPresenter class 我有MainPresenter

   private ItemModel _selectedItem;
   public ItemModel SelectedListItem
   {
            get => _selectedItem;  
            set
            {
                if (Equals(_selectedItem, value)) return;
                _selectedItem= value;
                RaisePropertyChangedEvent("SelectedListItem");
            }
        }
    public IEnumerable<ItemModel > ItemsList=> _dataStore.GetItems();
    public ICommand RemoveSelectedItem()
    {
       return  new RelayCommand<ItemModel>(RemoveSelectedItemFromList);
    }
    private void RemoveSelectedItemFromList(ItemModel item)
    {
            MessageDialogs.ShowInfoMessage(item.Name);
     }

List works perfectly, items are added and removed. 列表运行完美,添加和删除了项目。 SO binding works, but when I click Delete from the context menu nothing happens. SO绑定有效,但是当我从上下文菜单中单击“ Delete ,没有任何反应。 What could be wrong, I have tried almost everything. 可能出什么问题了,我几乎尝试了所有事情。 I guess that the problem is with DataContext, but how to fix it ? 我想问题出在DataContext,但是如何解决呢?

The problem lies with what you are binding against. 问题在于您要绑定的内容。

public ICommand RemoveSelectedItem()
{
   return  new RelayCommand<ItemModel>(RemoveSelectedItemFromList);
}
private void RemoveSelectedItemFromList(ItemModel item)
{
   MessageDialogs.ShowInfoMessage(item.Name);
}

You cannot apply a Binding against methods. 您不能对方法应用Binding You can only apply it to public properties. 您只能将其应用于公共财产。 Consequently, your ICommand binding should be 因此,您的ICommand绑定应为

private readonly ICommand _rsi = = new RelayCommand<ItemModel>(RemoveSelectedItemFromList);
public ICommand RemoveSelectedItem { get { return _rsi; } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM