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

Problem with MVVM + RadGridView's SelectedItem property

3 Answers 312 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Varun
Top achievements
Rank 1
Varun asked on 02 Nov 2011, 12:55 PM
Hi, I have posted my problem to the attachments that is relavent to the RadGridView and MVVM pattern.
Kindly address my problem.

3 Answers, 1 is accepted

Sort by
0
Tyree
Top achievements
Rank 2
answered on 02 Nov 2011, 03:36 PM
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.

0
Varun
Top achievements
Rank 1
answered on 03 Nov 2011, 10:29 AM
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.
0
Mark
Top achievements
Rank 1
answered on 09 Oct 2013, 09:09 PM
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>
Tags
GridView
Asked by
Varun
Top achievements
Rank 1
Answers by
Tyree
Top achievements
Rank 2
Varun
Top achievements
Rank 1
Mark
Top achievements
Rank 1
Share this question
or