将当前项目作为数据模板中的命令参数发送
Send current item as command parameter in data template
考虑以下 class:
public class Customer
{
public Int32 Id { get; set; }
public String FirstName { get; set; }
public String LastName { get; set; }
}
考虑以下视图模型(在 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>
我们想在包装面板项目中代表我们的客户。
一切正常,我们的 RemoveCustomer 命令可以通过在视图中单击按钮来调用。
但是,如何将当前客户作为命令参数发送?
我遇到了麻烦:
DataContext="{Binding ElementName=CustomersManagement, Path=DataContext }"
Command="{Binding RemoveCustomer }" CommandParamter="??????????"
试试这个:
<Button Content="DELETE!"
Command="{Binding ElementName=CustomersManagement, Path=DataContext.RemoveCustomer}"
CommandParameter="{Binding}"/>
请参考以下示例。
<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; }
}
}
考虑以下 class:
public class Customer
{
public Int32 Id { get; set; }
public String FirstName { get; set; }
public String LastName { get; set; }
}
考虑以下视图模型(在 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>
我们想在包装面板项目中代表我们的客户。 一切正常,我们的 RemoveCustomer 命令可以通过在视图中单击按钮来调用。
但是,如何将当前客户作为命令参数发送?
我遇到了麻烦:
DataContext="{Binding ElementName=CustomersManagement, Path=DataContext }"
Command="{Binding RemoveCustomer }" CommandParamter="??????????"
试试这个:
<Button Content="DELETE!"
Command="{Binding ElementName=CustomersManagement, Path=DataContext.RemoveCustomer}"
CommandParameter="{Binding}"/>
请参考以下示例。
<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; }
}
}