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

SelectedItem Binding Issue

5 Answers 166 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
develuser
Top achievements
Rank 1
develuser asked on 23 May 2012, 04:09 PM
Hello Telerik,

I think I've found an issue with the RadGridView SelectedItem.
When I bind it to my ViewModel, and set my property, the view never select the row.

Here is a little example to illustrate this :

My ViewModel

public class VisitorsViewModel : NotificationObject {
 
    VisitorViewModel _chuck = new VisitorViewModel() { ID = 2, FirstName = "Chuck", LastName = "Noris" };
    VisitorViewModel _selectedItem;
 
    public VisitorViewModel SelectedItem {
        get { return _selectedItem; }
        set {
            _selectedItem = value;
            RaisePropertyChanged( "SelectedItem" );
        }
    }
 
    public ObservableCollection<VisitorViewModel> Items { get; set; }
 
    public VisitorsViewModel() {
        Items = new ObservableCollection<VisitorViewModel>( new VisitorViewModel [] {
            new VisitorViewModel() { ID = 1, FirstName = "Bruce", LastName = "Lee" },
            _chuck,
            new VisitorViewModel() { ID = 3, FirstName = "Steven", LastName = "Seagal" },
            new VisitorViewModel() { ID = 4, FirstName = "Jean-Claude", LastName = "Van Damme" }
        } );
        SelectedItem = _chuck;
    }
}


My View :

<UserControl x:Class="GridViewSelectedItemTest.MainPage"
        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
    <Grid x:Name="LayoutRoot">
        <telerik:RadGridView x:Name="gvVisitor" ItemsSource="{Binding Items}" SelectionMode="Single" SelectionUnit="FullRow"
                             SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
    </Grid>
</UserControl>

If I want to select the right row, I need to do something like this :

public MainPage() {
    InitializeComponent();
    this.DataContext = new VisitorsViewModel();
    this.Loaded += new System.Windows.RoutedEventHandler( MainPage_Loaded );
}
 
void MainPage_Loaded( object sender, System.Windows.RoutedEventArgs e ) {
    gvVisitor.SelectedItems.Add( (DataContext as VisitorsViewModel).SelectedItem );
}

Greetings,
Laurent

5 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 23 May 2012, 04:21 PM
Hi Laurent,

Can you please clarify - you manipulate a selected items collection , but the selection mode is single . What is the desired result  - binding a single  or persisting multiple selected items in RadGridView.

Regards,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
develuser
Top achievements
Rank 1
answered on 23 May 2012, 04:37 PM
Hello Pavel,

Thanks for your quick reply.

I try to bind a single value to the GridView SelectedItem property in order to get the selection on that single row. But the GridView don't select whatever.

My binding

SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

My ViewModel property :

VisitorViewModel _selectedItem;
 
public VisitorViewModel SelectedItem {
    get { return _selectedItem; }
    set {
        _selectedItem = value;
        RaisePropertyChanged( "SelectedItem" );
    }
}

Greetings,
Laurent

0
Pavel Pavlov
Telerik team
answered on 25 May 2012, 04:21 PM
Hello Laurent ,

I have found the problem is related to the fact the selected item is being set at a stage when RadGridView has not yet build its Items collection. Since it can not find it there the first attempt of setting the selected visual fails. Further changes in the ViewModel would be reflected properly.
To avoid duplication I will continue further communication on this matter in your relevant support ticket.

Kind regards,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Rami Abughazaleh
Top achievements
Rank 1
answered on 27 Sep 2012, 03:20 AM
Hi.

I am experiencing the same issue and I am wondering what needs to be changed to resolve it?

Thank you.
0
Rami Abughazaleh
Top achievements
Rank 1
answered on 27 Sep 2012, 04:52 AM
Tags
GridView
Asked by
develuser
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
develuser
Top achievements
Rank 1
Rami Abughazaleh
Top achievements
Rank 1
Share this question
or