This is a migrated thread and some comments may be shown as answers.

How to bound a Button placed on Telerik's RowDetailsTemplate to a ICommand in view's ViewModel

3 Answers 112 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 11 Apr 2012, 09:38 PM
Considering this code: 
<telerik:RadGridView ItemsSource="{Binding SearchResults}" RowDetailsVisibilityMode="VisibleWhenSelected">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="First Name" DataMemberBinding="{Binding FirstName}"  />
        <telerik:GridViewDataColumn Header="Last Name" DataMemberBinding="{Binding LastName}"  />
    </telerik:RadGridView.Columns>
    <telerik:RadGridView.RowDetailsTemplate>
        <DataTemplate>
            <Button Command="{Binding OpenView1Command}">Open View 1</Button> <!-- This does not work -->
        </DataTemplate>
    </telerik:RadGridView.RowDetailsTemplate>
</telerik:RadGridView>


The OpenView1Command is defined in my ViewModel. How do I make the binding work? 

3 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 12 Apr 2012, 07:05 AM
Hi Michael,

Since the DataContext of the RowDetails is not the one of the whole grid, but rather the corresponding item, the command from the view model cannot be found. What you can try is to expose your ViewModel as a resource and use it a Source of the binding of the command:

<Button Command="{Binding OpenView1Command, Source="{StaticResource MyViewModel}"}">Open View 1</Button>
 

Regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Michael
Top achievements
Rank 1
answered on 12 Apr 2012, 02:27 PM
Thanks for your reply.
Can you give me a sample on how to "expose your ViewModel as a resource"?
0
Sergiy
Top achievements
Rank 1
answered on 19 Sep 2012, 07:41 PM
But if I don't use MyViewModel as xaml resoulre, how I can do it?.
I am using "datacontext = ViewModel" in code.
I can use ButtonName.DataContext = VM in code, because Button insite DataTemplate, and I can't get it by Name.

Tags
GridView
Asked by
Michael
Top achievements
Rank 1
Answers by
Maya
Telerik team
Michael
Top achievements
Rank 1
Sergiy
Top achievements
Rank 1
Share this question
or