考虑以下课程:
public class Customer
{
public Int32 Id { get; set; }
public String FirstName { get; set; }
public String LastName { get; set; }
}
考虑遵循View Model(在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
}
}
}
并考虑以下观点:
<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>
我们想在Wrap面板项目中代表我们的客户。 一切正常,我们的RemoveCustomer命令可以从视图中的按钮单击调用。
但是,如何将当前客户作为Command参数发送?
我在这里遇到麻烦:
DataContext="{Binding ElementName=CustomersManagement, Path=DataContext }"
Command="{Binding RemoveCustomer }" CommandParamter="??????????"
答案 0 :(得分:11)
试试这个:
<Button Content="DELETE!"
Command="{Binding ElementName=CustomersManagement, Path=DataContext.RemoveCustomer}"
CommandParameter="{Binding}"/>
答案 1 :(得分:4)
请参考以下示例。
<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; }
}
}