SelectedItem Binding Issue

6 posts, 0 answers
  1. develuser
    develuser avatar
    14 posts
    Member since:
    Dec 2011

    Posted 23 May 2012 Link to this post

    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
  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 23 May 2012 Link to this post

    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 >>

  3. DevCraft banner
  4. develuser
    develuser avatar
    14 posts
    Member since:
    Dec 2011

    Posted 23 May 2012 Link to this post

    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

  5. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 25 May 2012 Link to this post

    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 >>

  6. Rami Abughazaleh
    Rami Abughazaleh avatar
    27 posts
    Member since:
    Feb 2010

    Posted 26 Sep 2012 Link to this post

    Hi.

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

    Thank you.
  7. Rami Abughazaleh
    Rami Abughazaleh avatar
    27 posts
    Member since:
    Feb 2010
Back to Top
DevCraft banner