C# MVVM命令选择最后添加的项
我有一个带有命令的viewmodel。但只有最后添加的项可由命令编辑。当你看颂歌时,这是有道理的。但这不是我想要的。我想编辑所选项目。我将概述我的问题: 我有一个模型,名为PartC# MVVM命令选择最后添加的项,c#,wpf,mvvm,command,C#,Wpf,Mvvm,Command,我有一个带有命令的viewmodel。但只有最后添加的项可由命令编辑。当你看颂歌时,这是有道理的。但这不是我想要的。我想编辑所选项目。我将概述我的问题: 我有一个模型,名为Part public class Part { private string _partcode; private string _description; public string PartCode { get { return _partcode.Trim(); }
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,名为PartViewModel
/// <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
}
如何将命令绑定到选定项。我想一定是viewmodel有问题,而不是绑定中的问题_每次添加新零件时,零件get都会被覆盖。但是我如何更改它才能正常工作呢?使用接受部分
命令参数的RelayCommand
:
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;
}
公共ICommand更新说明
{
得到
{
返回新的RelayCommand(UpdateDescriptionExecute、CanUpdateDescriptionExecute);
}
}
私有void UpdateDescriptionExecute(部分)
{
MessageBox.Show(part.PartCode);
}
私有布尔CanUpdateDescriptionExecute(部分)
{
如果(计数>=2)
返回false;
其他的
返回true;
}
并稍微修改命令绑定:
<Button Content="Update" Command="{Binding DataContext.UpdateDescription,ElementName=TestView}" CommandParameter="{Binding}"/>
我知道您要去哪里,但现在我得到了一个NullReferenceException。UpdateDescriptionExecure中的“part”参数为null,表示它没有传递选定的部分。有什么想法吗?您是否从XAML标记中的元素中删除了DataContext属性?用我发布的示例标记替换您的按钮,然后重试。谢谢!忽略了那个。还有一个问题,但这将是一个新的话题。。
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;
}
<Button Content="Update" Command="{Binding DataContext.UpdateDescription,ElementName=TestView}" CommandParameter="{Binding}"/>