Problem with MVVM + RadGridView's SelectedItem property

4 posts, 0 answers
  1. Varun
    Varun avatar
    3 posts
    Member since:
    Nov 2011

    Posted 02 Nov 2011 Link to this post

    Hi, I have posted my problem to the attachments that is relavent to the RadGridView and MVVM pattern.
    Kindly address my problem.
  2. Tyree
    Tyree avatar
    105 posts
    Member since:
    Dec 2006

    Posted 02 Nov 2011 Link to this post

    I personally don't use SelectedItem in MVVM. I believe the "better" option is to use a collection that implements ICollectionView. I personally use CollectionViewSource but you can use others or implement your own. At this point I feel you have better control and response to the changing selected item and the collection as a whole. It also allows you to change the sort and filter all in the VM with no concern of actually altering the underlying collections.

    In your case when you click the checkbox its going to change the Pesticides[i].IsChecked property to true or false. PesticideItem, which is a property on your main VM will not be set unless you are changing which item is selected via the View. So if you select a different row in your grid the SelectedItem should update and then perhaps your PesticideItem will reflect that change but clicking the checkbox IsChecked will not necessarily do it on its own. You could do something in your IsChecked setter to bubble up to the main VM to tell it which item you just clicked on.

    Since you have a check box in a celltemplate I believe you may be blocking the click event which would normally set the row to SelectedItem. Have you set some debug events to see if the grid thinks the correct row is the SelectedItem? If you click the row without clicking the checkbox does it do what you want?

    If you added a PesticidesListView property of type CollectionViewSource, and set the .Source property to PesticidesList, then bound your grid to CollectionViewSource.View instead of PesticidesList, you could then look at the CollectionViewSource.CurrentItem property or watch the CollectionViewSource.CurrentItemChanged or CurrentItemChanging events to respond.

  3. DevCraft banner
  4. Varun
    Varun avatar
    3 posts
    Member since:
    Nov 2011

    Posted 03 Nov 2011 Link to this post

    Hi,
        Thanks for the detailed explanation. Here is the outcome after your recommendations:
    1: As i am implementing MVVM pattern, handling events in MVVM is something that is not currently followed in our project. So, even if i bubble the event up the hierarchy in the tree view, my VM doesn't know about the event raised.
    2: Also, after binding the CollectionView to the ItemSource property, the CurrentItem is never updated whenever a CheckBox is either checked\unchecked.

    Hence, we followed another approach of defining an action to the Command Property.
    3: Because of point1 , what we did is, binded the Command property and defined an action in the ActionCommand constructor, to do the desired action.
  5. Mark
    Mark avatar
    15 posts
    Member since:
    Apr 2012

    Posted 09 Oct 2013 Link to this post

    Hello Telerik.  Do you have a fix or workaround for the problem Varun describes which is that SelectedItem does not change when I click on the checkbox in the column bound to IsDesignatedBestJob in a gridview row defined thusly:

    <Grid.Resources>
      <Style x:Key="GridViewCellStyle" TargetType="telerik:GridViewCell">
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="telerik:StyleManager.Theme" Value="Windows7"/>
      </Style>
    </Grid.Resources>
    <telerik:RadGridView
      AlternateRowBackground="AliceBlue"
      AlternationCount="2"
      AutoGenerateColumns="False"
      HorizontalContentAlignment="Center"
      IsFilteringAllowed="False"
      IsSynchronizedWithCurrentItem="True"
      ItemsSource="{Binding Path=JobListView}"                                               
      RowIndicatorVisibility="Collapsed"
      SelectedItem="{Binding Path=Model.SelectedJob,Mode=TwoWay}"                        
      SelectionMode="Single"               
      ShowGroupPanel="False"
      >
      <telerik:RadGridView.Columns>
       <telerik:GridViewDataColumn 
          CellStyle="{StaticResource GridViewCellStyle}"
          DataMemberBinding="{Binding IsDesignatedBestJob,Mode=TwoWay}"
          Header="IsBestJob" 
          HeaderTextAlignment="Center"
          IsReadOnly="True"
          >
          <telerik:GridViewDataColumn.CellTemplate>
            <DataTemplate>
              <CheckBox IsChecked="{Binding IsDesignatedBestJob, Mode=TwoWay}"/>
            </DataTemplate>
          </telerik:GridViewDataColumn.CellTemplate>
        </telerik:GridViewDataColumn>
      </telerik:RadGridView.Columns>
    </telerik:RadGridView>
Back to Top