【发布时间】:2017-10-30 16:24:06
【问题描述】:
我有一个ListView,现在在SelectedItem 上打开一个弹出窗口。
我想要的是,如果用户决定从列表中删除一个项目,他可以单击按钮并将其删除 - 现在按钮确实会触发,但是我如何告诉 VM 中的按钮要删除哪些项目 - 没有“选定项目”? p.e..
<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);
}
}
【问题讨论】: