c# - MVVM 命令选择最后添加的项目

标签 c# wpf mvvm command

我有一个带有命令的 View 模型。但只有最后添加的项目可以通过该命令进行编辑。当你看颂歌时,这是有道理的。但这不是我想要的。我希望编辑所选项目。我将勾勒出我的问题:

我有一个模型,名为 部分

 public class Part
 {
    private string _partcode;
    private string _description;

    public string PartCode
    {
        get { return _partcode.Trim(); }
        set { _partcode = value; }
    }

    public string Description
    {
        get { return _description; }
        set { _description = value; }
    }
  }

带有命令的 ViewModel,名为 零件 View 模型
    /// <summary>
    /// Returns a ViewModel containing all parts.
    /// </summary>
    /// <param name="dt">Database to use.</param>
    public PartViewModel(DatabaseType dt)
    {
        GenerateViewModelForAllParts(dt);
    }
    private async void GenerateViewModelForAllParts(DatabaseType dt)
    {
        using (NexusWCFServiceClient client = new NexusWCFServiceClient())
            foreach (Part item in await client.GetAllPartsAsync(dt))
            {
                _part = item;
                _items.Add(item);
            }
    }
    #endregion

    #region Members
    private ObservableCollection<Part> _items = new ObservableCollection<Part>();

    private Part _part;
    int count = 0;
    #endregion

    #region Properties
    public ObservableCollection<Part> Items
    {
        get { return _items; }
        set { _items = value; }
    }

    public Part Part
    {
        get { return _part; }
        set { _part = value; }
    }

    public string PartCode
    {
        get { return Part.PartCode; }
        set
        {
            if (Part.PartCode != value) /* Check if value is changed */
            {
                Part.PartCode = value;
                RaisePropertyChanged("PartCode");  /* Raise event */
            }
        }
    }

    public string Description
    {
        get { return Part.Description; }
        set
        {
            if (Part.Description != value)
            {
                Part.Description = value;
                RaisePropertyChanged("Description");
            }
        }
    }

    #region Commands
    private void UpdateDescriptionExecute()
    {
        //count++;
        //Description = Description + count.ToString();
        // Part.Description = "asdasdasd";
        MessageBox.Show(PartCode);
    }

    private bool CanUpdateDescriptionExecute()
    {
        if (count >= 2)
            return false;
        else
            return true;
    }

    public ICommand UpdateDescription
    {
        get
        {
            return new RelayCommand(UpdateDescriptionExecute, CanUpdateDescriptionExecute);
        }
    }
    #endregion
}

现在,每当我尝试在 View 中触发命令时:
<Window x:Class="NexusWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="700" Width="525" x:Name="TestView">
    <Grid>
        <StackPanel x:Name="stck">
            <ListView x:Name="lb" ItemsSource="{Binding Items}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Vertical" x:Name="stck">
                            <TextBlock Text="{Binding Path=PartCode}"/>
                            <TextBlock Text="{Binding Path=Description}"/>
                            <Button Content="Update" DataContext="{Binding ElementName=TestView, Path=DataContext}" Command="{Binding UpdateDescription}" CommandParameter="{Binding}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackPanel>
    </Grid>
</Window>

最后添加的项目(部件)被编辑。这是有道理的,因为 ViewModel 中有以下代码:
 _part = item;

如何将命令绑定(bind)到所选项目。我想 viewmodel 一定有问题,而不是绑定(bind)。 _part get 每次添加新部件时都会被覆盖。但是我怎样才能改变它,让它起作用呢?

最佳答案

使用 RelayCommand<Part>接受 Part命令参数:

public ICommand UpdateDescription
{
    get
    {
        return new RelayCommand<Part>(UpdateDescriptionExecute, CanUpdateDescriptionExecute);
    }
}

private void UpdateDescriptionExecute(Part part)
{
    MessageBox.Show(part.PartCode);
}

private bool CanUpdateDescriptionExecute(Part part)
{
    if (count >= 2)
        return false;
    else
        return true;
}

并稍微修改 Command 绑定(bind):
<Button Content="Update" Command="{Binding DataContext.UpdateDescription,ElementName=TestView}" CommandParameter="{Binding}"/>

关于c# - MVVM 命令选择最后添加的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41853312/

相关文章:

c# - Automapper 在第一次调用时正确映射,但在第二次调用时跳过属性

c# - WPF - 设置 IsOpen=true 时工具提示数据绑定(bind)不起作用

c# - WPF ObservableCollection : How to add a blank line in one form's combobox, 但实际上不影响ObservableCollection?

wpf - 是否有任何MVVM库实现CommandSinkBinding?

ios - 使用 ReactiveCocoa 的 iOS 应用程序的 ViewModel 模式

mvvm - 无法调用未定义的方法 'push'

javascript - Angularjs 指令和 string.format

c# - 菜单快捷键范围

ios - 将 UITextField 或按钮与 viewModel 绑定(bind)

C# 等同于生锈 "match"