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

Muliple items keep selecting even with SelectionMode="Single"

3 Answers 79 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ken
Top achievements
Rank 1
Ken asked on 10 Mar 2011, 08:13 PM
Hi there, 

I am using a RadGridView, here is the XAML

<telerik:RadGridView Grid.Row="1"
                            Grid.ColumnSpan="3"
                            SelectionMode="Single"
                            IsReadOnly="True"
                            Name="radGridView1"
                            ItemsSource="{Binding Path=ModelCodeList}"
                            SelectedItem="{Binding Path=SelectedModel, Mode=TwoWay}"
                            AutoGenerateColumns="False"  >
           <telerik:RadGridView.Columns>
               <telerik:GridViewDataColumn DataMemberBinding="{Binding Code}" Header="Model"/>
               <telerik:GridViewDataColumn DataMemberBinding="{Binding ID}" Header="ID"/>
               <telerik:GridViewDataColumn DataMemberBinding="{Binding OemName}" Header="OEM Name"/>
               <telerik:GridViewDataColumn DataMemberBinding="{Binding ModalityCode}" Header="Modality Code"/>
               <telerik:GridViewDataColumn DataMemberBinding="{Binding Active}" Header="Model Active"/>
           </telerik:RadGridView.Columns>
       </telerik:RadGridView>

As you can see I am binding the selected item, I followed the example from the code download in this (http://www.telerik.com/community/forums/wpf/gridview/select-a-row-from-viewmodel.aspx) post. I works the first time no problem... I put a breakpoint in my view model and I can see my SelectedModel getting set. The problem is that I can't unselect the first item and have it set the second item. At this point I am stuck - here is the pertinent code from the view model.

public Model SelectedModel
   {
     get { return selectedModel; }
     set
     {
       if (selectedModel != value)
       {
         selectedModel = value;
       }
       RaisePropertyChanged(() => this.SelectedModel);
       AddSelectedModelCommand.RaiseCanExecuteChanged();
     }
   }


What is does is keeps highlighting rows - even though I have SelectionMode="sinlge" - Uhm? Any suggestions? I attached a screen capture to show what I mean - see all the highlighted rows?

Thanks!

** Also - The only difference I can see between my project and the sample is that we are "Target Framework:" is set to ".NET Framework 4" and your project is being built as ".NET Framework 4 Client Profile"

3 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 11 Mar 2011, 09:08 AM

Hi Ken,

Do you data items override GetHashCode and Equals methods by any chance ? If that is the case, problems like the one that you have mentioned can occur if several items have the same hash code. Currently the grid expects that hash codes are unique.



Kind regards,
Milan
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Ken
Top achievements
Rank 1
answered on 11 Mar 2011, 03:54 PM
Yeah - it does...
public override bool Equals(object obj)
  {
    bool match = false;
 
    if (obj.GetType() == typeof(Model))
    {
      Model m = (Model)obj;
 
      match = Active != m.Active
        || Code != m.Code
        || Description != m.Description
        || ModalityId != m.ModalityId
        || OemId != m.OemId
        || ParentID != m.ParentID;
    }
 
    return match;
  }

Is there something we could change in the Equals method that would fix it?

Thanks

0
Milan
Telerik team
answered on 17 Mar 2011, 03:45 PM
Hi Ken,

You could try changing the Equals to :

public override bool Equals(object obj)
{
    return ReferenceEquals(this, obj);
}

Hope this helps.

Regards,
Milan
the Telerik team
Tags
GridView
Asked by
Ken
Top achievements
Rank 1
Answers by
Milan
Telerik team
Ken
Top achievements
Rank 1
Share this question
or