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

[Solved] Commanding

7 Answers 181 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Chris
Top achievements
Rank 1
Chris asked on 03 Jun 2010, 05:12 PM
Hi,

I'm having a challenge getting the commanding to work (MVVM approach) with a Button inside the RadGridView.

My column is as follows:

<telerikGridView:GridViewDataColumn Width="50" >

 

 

 

 

<telerikGridView:GridViewDataColumn.CellTemplate>

 

 

 

 

<DataTemplate>

 

 

 

 

<Button x:Name="EditButton" Command="{Binding Path=LayoutRoot, Path=DataContext.EditRecordCommand}" Content="Edit" Width="40" Height="20" FontSize="11" FontWeight="Normal" MinWidth="1" />

 

 

 

 

</DataTemplate>

 

 

 

 

</telerikGridView:GridViewDataColumn.CellTemplate>

 

 

 

 

</telerikGridView:GridViewDataColumn>


My code however does not seem to execute. 

Is this a known issue?  I'm using the SL 4 version of the controls with VS2010.

Regards,

Chris

7 Answers, 1 is accepted

Sort by
0
Yavor Georgiev
Telerik team
answered on 03 Jun 2010, 05:31 PM
Hi Chris,

 Your Command needs to be bound like this:

Command="{Binding Path=DataContext.EditRecordCommand, ElementName=LayoutRoot}"

Kind regards,
Yavor Georgiev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Chris
Top achievements
Rank 1
answered on 03 Jun 2010, 05:44 PM
Hi Yavor,

Sorry, I noticed I had retyped the ElementName and typed Path by mistake in my sample code below.

I had the same code as you and that did not work.

This is my code...

 

 

 

<telerikGridView:GridViewColumn Width="50" >

 

 

 

 

<telerikGridView:GridViewColumn.CellTemplate>

 

 

 

 

<DataTemplate>

 

 

 

 

<Button x:Name="EditButton" Command="{Binding Path=DataContext.EditRecordCommand, ElementName=LayoutRoot}" Content="Edit" Width="40" Height="20" FontSize="11" FontWeight="Normal" MinWidth="1" />

 

 

 

 

</DataTemplate>

 

 

 

 

</telerikGridView:GridViewColumn.CellTemplate>

 

 

 

 

</telerikGridView:GridViewColumn>

 



Regards,

Chris
0
Yavor Georgiev
Telerik team
answered on 04 Jun 2010, 11:18 AM
Hi Chris,

 If you define your ViewModel like this:

<UserControl.Resources>
    <my:MyViewModel x:Key="MyViewModel"/>
</UserControl.Resources>

You can the access it like so:
<telerik:GridViewColumn Header="Actions">
                   <telerik:GridViewColumn.CellTemplate>
                       <DataTemplate>
                           <Button Command="{Binding SaveCommand, Source={StaticResource MyViewModel}}" CommandParameter="{Binding}">Save</Button>
                       </DataTemplate>
                   </telerik:GridViewColumn.CellTemplate>
               </telerik:GridViewColumn>

All the best,
Yavor Georgiev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Chris
Top achievements
Rank 1
answered on 04 Jun 2010, 12:18 PM
Does it not work by setting the view model as the DataContext of the page?  I would prefer to use it this way as my page is already set up this way.

Regards,

Chris
0
Yavor Georgiev
Telerik team
answered on 04 Jun 2010, 12:23 PM
Hi Chris,

 Instantiating the ViewModel as a resource does not preclude setting it as a DataContext in the page:

<UserControl ...
DataContext="{StaticResource MyViewModel}">

I'm afraid instantiating it as a a resource is the only way to achieve your scenario.

All the best,
Yavor Georgiev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Josh
Top achievements
Rank 1
answered on 24 Sep 2010, 09:32 PM
What if your ViewModel does not have a parameterless constructor?
0
Yavor Georgiev
Telerik team
answered on 24 Sep 2010, 10:27 PM
Hello Josh,

 In this case, you need to use a DataContextProxy.

All the best,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Chris
Top achievements
Rank 1
Answers by
Yavor Georgiev
Telerik team
Chris
Top achievements
Rank 1
Josh
Top achievements
Rank 1
Share this question
or