Databinding to Selected Cells collection

2 posts, 0 answers
  1. Andy
    Andy avatar
    16 posts
    Member since:
    Aug 2012

    Posted 13 Feb 2012 Link to this post


    I have a RadGridView from which I have set up an EventTrigger using the MVVMLight EventToCommand object in order to trap the SelectionChanged event in my ViewModel. This works fine when I pass in the DataGrid as an object to the ViewModel. Below is my Trigger:
        <i:EventTrigger EventName="SelectionChanged">
            <cmd:EventToCommand  CommandParameter="{Binding ElementName=RadGrid}" Command="{Binding SelectPriotityCommand}"  PassEventArgsToCommand="False"/>



    However when I use this pattern I then have to derive the object I am interested in from the SelectedCells collection of the Grid. Also I would rather not access the RadGridView from the ViewModel at all as this is difficult to Unit test. Therefore I would prefer to get a handle on the selected item in the collection from XAML. Should be simple! However if I substitute the CommandParemeter above with the following:


    Binding ElementName='RadGrid', Path='SelectedCells[0].Item'}


    Then I get the error

    System.Windows.Data Error: Failed to connect to index '0' in object 'Telerik.Windows.Controls.GridView.Selection.CellInfoCollection' (type 'Telerik.Windows.Controls.GridView.Selection.CellInfoCollection'). BindingExpression: Path='SelectedCells[0].Item'

    This seems to happen as soon as the page is loaded, am I simply getting this error as the collection is empty? How do I get this to work? I feel I must be missing something rather simple?



  2. Vlad
    Vlad avatar
    11100 posts

    Posted 14 Feb 2012 Link to this post


    Maybe cells collection is empty (you do not have item at index 0) in your case?

    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top