Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Datatemplate of listboxItem. in Datatemplate I place a button and I want bind command of Button to ViewModel of UserControl (listbox inside that UserControl).

I tried solutions found on internet, but all did not work.

is there anyone who can provide me a artcile or tips to do that?

thanks in advance!
Posted
Comments
SteveAdey 3-Apr-12 17:32pm    
Can you post some code, as this should work.

1 solution

Here is how to do so.
XML
<datatemplate x:key="CommandsTemplate" xmlns:x="#unknown">
 <itemscontrol itemssource="{Binding Path=Commands}">
   <itemscontrol.itemtemplate>
     <datatemplate>
       <textblock margin="2,6">
         <button command="{Binding Path=Command}">
           <textblock text="{Binding Path=DisplayName}" />
         </button>
       </textblock>
     </datatemplate>
   </itemscontrol.itemtemplate>
 </itemscontrol>
</datatemplate>

C#
 public class CommandViewModel : ViewModelBase
{
    public CommandViewModel(string displayName, ICommand command)
    {
        if (command == null)
            throw new ArgumentNullException("command");

        base.DisplayName = displayName;
        this.Command = command;
    }

    public ICommand Command { get; private set; }
}

For further read MVVM pattern[^] from MSDN
 
Share this answer
 
v2
Comments
Alimjan Yasin 12-Apr-12 23:22pm    
the article you provide hard to fellow, could you please provide more simple one?
Wonde Tadesse 24-Apr-12 19:35pm    
See the link in the solution. It's self explanatory.
VJ Reddy 23-Apr-12 19:28pm    
Good answer and reference. 5!
Wonde Tadesse 24-Apr-12 19:35pm    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900