2015-02-08 37 views
3

请考虑下面的类:数据模板发送当前项目的命令参数

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命令可以从按钮点击视图中调用。

但是,如何将当前客户作为Command参数发送?

我在这里已经一个麻烦:

DataContext="{Binding ElementName=CustomersManagement, Path=DataContext }" 
    Command="{Binding RemoveCustomer }" CommandParamter="??????????" 

回答

3

请参考下面的示例。

<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; } 
    } 
} 
+0

@请问你能提供更多的细节吗? – 2015-02-09 22:08:49

9

试试这个:

<Button Content="DELETE!" 
     Command="{Binding ElementName=CustomersManagement, Path=DataContext.RemoveCustomer}" 
     CommandParameter="{Binding}"/> 
+0

结合于整体的DataContext不是对象,所述的DataTemplate被绑定到。 – rolls 2017-07-28 04:22:34