c# - UWP ListView 按钮 MVVM 绑定(bind)

标签 c# listview mvvm uwp

我有一个 ListView那,现在在 SelectedItem 上打开一个弹出窗口.
我想要的是,如果用户决定从列表中删除一个项目,他可以单击按钮并将其删除 - 现在按钮确实会触发,但是我如何告诉 VM 中的按钮要删除哪些项目 - 没有“选定项目”?体育...

<ListView 
 SelectedItem="{Binding...}"
 x:Name="lv">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Image Source="{Binding...}"/>
                <Button Command="{Binding ElementName=lv,Path=DataContext.RemoveXCommand}" />
            </Stackpanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

虚拟机
public void RemoveXCommand()
    {
    foreach(var item in pseudo)
       {
       if(item.Name == ?????)
           pseudo.Remove(item);
       }
    }

有没有办法,或者我必须删除Popup的打开,并将其实现为另一个Button,这样我才能使用SelectedItem进行比较?

谢谢你。

编辑1:

感谢 Fruchtzwerg我让它工作了
public RelayCommand<string> RemoveXCommand{ get; private set; }

//... in then Constructor
RemoveXCommand = new RelayCommand<string>((s) => RemoveXCommandAction(s));

public void RemoveXCommand(object temp)
{
foreach(var item in pseudo)
   {
   if(item.Name == (string) temp)
       pseudo.Remove(item);
   }
}

最佳答案

您可以将需要删除的项目传递为 CommandParameter

<Button Command="{Binding ElementName=lv, Path=DataContext.RemoveXCommand}"
        CommandParameter="{Binding}"/>

并像删除它一样
public void RemoveXCommand(object itemToRemove)
{
    pseudo.Remove(itemToRemove);
}

您也可以按名称删除项目。绑定(bind)Name项目的 CommandParameter
<Button Command="{Binding ElementName=lv, Path=DataContext.RemoveXCommand}"
        CommandParameter="{Binding Name}"/>

并像删除它一样
public void RemoveXCommand(object nameToRemove)
{
    foreach(var item in pseudo)
    {
        if(item.Name == (string)nameToRemove)
        {
            pseudo.Remove(item);
        }
    }
}

请注意,第二种方法是删除具有您选择的项目名称的所有项目。第一种方法仅删除您选择的项目,因为特定实例已被删除。

允许 RelayCommand 中的参数ICommand 的新的或修改的实现是必须的。这是一个可能的解决方案:
public class ParameterRelayCommand : ICommand
{
    private readonly Action<object> _execute;
    private readonly Func<bool> _canExecute;

    public event EventHandler CanExecuteChanged;

    public ParameterRelayCommand(Action<object> execute)
        : this(execute, null)
    { }

    public ParameterRelayCommand(Action execute<object>, Func<bool> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");
        _execute = execute;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute();
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    public void RaiseCanExecuteChanged()
    {
        var handler = CanExecuteChanged;
        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }
    }
}

关于c# - UWP ListView 按钮 MVVM 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44258086/

相关文章:

android - 如何为 ListItem 制作自定义上下文菜单(如在 Baconreader 中)?

android - Cursor Adapter 的用途是什么?

c# - 有没有办法分解 Expression<Func<T, Bool>> 并获得相等比较的右侧?

c# - 如何检测 Excel 是否通过自动化启动(VSTO Addin 上下文)

c# - TPL 数据流 - 随时控制流中的项目

c# - WPF MVVM 从 icommand 执行更改父窗口 View 模型

wpf - 使用mvvm light从数据库交换按钮上的图像

c# - 如何在代码中绑定(bind)GridViewColumn的DisplayMemberBinding

java - 如何设置 ListView 中项目的背景颜色

WPF itemscontrol 子对齐