Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send current item as command parameter in data template

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)

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.

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="??????????"
like image 513
Yaser Moradi Avatar asked Feb 08 '15 16:02

Yaser Moradi


2 Answers

Try this:

 <Button Content="DELETE!" 
         Command="{Binding ElementName=CustomersManagement, Path=DataContext.RemoveCustomer}" 
         CommandParameter="{Binding}"/>
like image 91
Federico Berasategui Avatar answered Nov 08 '22 10:11

Federico Berasategui


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; }
    }
}
like image 28
Ayyappan Subramanian Avatar answered Nov 08 '22 08:11

Ayyappan Subramanian