wpf 将当前项目作为数据模板中的命令参数发送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28396481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send current item as command parameter in data template
提问by Yaser Moradi
Consider following class:
考虑以下类:
public class Customer
{
public Int32 Id { get; set; }
public String FirstName { get; set; }
public String LastName { get; set; }
}
Consider following View Model (In MVVM design pattern)
考虑遵循视图模型(在 MVVM 设计模式中)
public class CustomersManagementViewModel
{
public Command<Customer> RemoveCustomer;
public List<Customer> Customers { get; set; }
CustomersManagementViewModel()
{
Customers = new Customers {
new Customer {},
new Customer {}
}
RemoveCustomer = new Command<Customer>((customerToRemove) => {
// do something with that passed customer
}
}
}
And consider following view:
并考虑以下观点:
<Window x:Class="View.CustomersManagementView"
xmlns:vm="clr-namespace:ViewModel;assembly=ViewModel"
Name="CustomersManagement">
<Window.DataContext>
<vm:SmsManagementViewModel />
</Window.DataContext>
<StackPanel>
<ItemsControl ItemsSource="{Binding Customers}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding FirstName}" />
<Label Content="{Binding LastName }" />
<Button Content="DELETE!" DataContext="{Binding ElementName=CustomersManagement , Path=DataContext }" Command="{Binding RemoveCustomer }" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
</Window>
We'd like to represent our customers in Wrap panel items. Everything works fine, and our RemoveCustomer command can be called from button click in view.
我们想在 Wrap 面板项目中代表我们的客户。一切正常,我们的 RemoveCustomer 命令可以从视图中的按钮单击调用。
But, how can I send current customer as Command parameter ?
但是,如何将当前客户作为命令参数发送?
I've a trouble here:
我在这里遇到了麻烦:
DataContext="{Binding ElementName=CustomersManagement, Path=DataContext }"
Command="{Binding RemoveCustomer }" CommandParamter="??????????"
回答by Federico Berasategui
Try this:
尝试这个:
<Button Content="DELETE!"
Command="{Binding ElementName=CustomersManagement, Path=DataContext.RemoveCustomer}"
CommandParameter="{Binding}"/>
回答by Ayyappan Subramanian
Please refer the below sample.
请参考以下示例。
<Window x:Class="DelegateComd_Learning.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" x:Name="CustomersManagement" >
<Window.Resources>
</Window.Resources>
<StackPanel>
<ItemsControl ItemsSource="{Binding Persons}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" x:Name="stck">
<Label Content="{Binding FirstName}" />
<Label Content="{Binding LastName }" />
<Button Content="DELETE!" DataContext="{Binding ElementName=CustomersManagement , Path=DataContext }" Command="{Binding RemoveCustomer }"
CommandParameter="{Binding ElementName=stck,Path=DataContext}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new TestViewModel();
}
}
class TestViewModel
{
private ObservableCollection<Person> myVar;
public ObservableCollection<Person> Persons
{
get { return myVar; }
set { myVar = value; }
}
public DelegateCommand<Person> RemoveCustomer { get; set; }
public TestViewModel()
{
Persons = new ObservableCollection<Person>();
for (int i = 0; i < 10; i++)
{
Persons.Add(new Person() { FirstName = "Test"+i, LastName = "Testlst"+i });
}
RemoveCustomer = new DelegateCommand<Person>(Removeperson);
}
private void Removeperson(Person prps)
{
}
}
public class Person
{
private string firstName;
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
private string lastName;
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
}

