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

Binding Row Selection to Command for MVVM pattern?

3 Answers 292 Views
GridView
This is a migrated thread and some comments may be shown as answers.
M
Top achievements
Rank 1
M asked on 10 Jul 2009, 02:47 PM
I have a telerik RadGridView that I would like to bind the row selection to a ICommand object, so my viewModel can execute it's code. I obviously cannot bind the SelectionChanged Event to a command, so I was wondering if there was another way to do this currently? Thank you for any assistance.

3 Answers, 1 is accepted

Sort by
0
Accepted
Casey Watson
Top achievements
Rank 1
answered on 10 Jul 2009, 03:15 PM
What I do is bind a property in my presentation model to the selected item and then fire an event when the selected item changes:

 

<telerik:RadGridView x:Name="List" AutoGenerateColumns="False" ItemsSource="{Binding MyList.Items}" SelectedItem="{Binding CurrentListItem, Mode=TwoWay}" >

 

public ListItem CurrentListItem

{

 

get

 

 

 

 

 

 

 

return currentListItem;

 

 

}

 

 

set

 

 

{

 

 

 

if (currentListItem != value)

 

 

{

currentListItem =

value;

 

 

 OnPropertyChanged(

 

"CurrentListItem"); 
if (currentListItem != null)  

{

eventAggregator.GetEvent<

ListItemSelectedEvent>().Publish(CurrentListItem.Id);

 

 

            }

        }

    }

}

 

 

0
M
Top achievements
Rank 1
answered on 10 Jul 2009, 03:28 PM
Thanks, this solution works, but doesn't feel quite MVVM.  Seems like the controls could use a little tweaking to really harness the full power of WPF's data binding, but overall I am happy with what I am able to accomplish.
0
Casey Watson
Top achievements
Rank 1
answered on 14 Jul 2009, 02:09 PM
I'm glad I could help. This is the way that Microsoft does a similar thing in their reference implementation that comes with the Composite Application Guidance which generally follows MVVM. What about it does not quite feel right to you? If you come up with a better solution, please post it here.
Tags
GridView
Asked by
M
Top achievements
Rank 1
Answers by
Casey Watson
Top achievements
Rank 1
M
Top achievements
Rank 1
Share this question
or