RadListPicker SelectedItems MVVM

2 posts, 0 answers
  1. Vladimir
    Vladimir  avatar
    7 posts
    Member since:
    Dec 2012

    Posted 07 Dec 2012 Link to this post

    How to get/set SelectedItems via MVVM? How to bind Items collection?
    My XAML code
    <
    telerikInput:RadListPicker
                            x:Name="CountryPicker"
                            ItemsSource="{Binding TitleCountries}"                       
                            Header="Country"
                            SelectionMode="Multiple"   
                            SelectedItem="{Binding SelectedCountries,Mode=TwoWay}"
                            OkButtonIconUri="/Toolkit.Content/ApplicationBar.Check.png"
                            CancelButtonIconUri="/Toolkit.Content/ApplicationBar.Cancel.png"
                            PopupHeader="">

    and ViewModel
    ObservableCollection<Country> _TitleCountries;
          public ObservableCollection<Country> TitleCountries
          {
              get
              {
                  return _TitleCountries;
              }
     
          }
     
          ObservableCollection<Country> _SelectedCountries;
          public ObservableCollection<Country> SelectedCountries
          {
              get
              {
                  return _SelectedCountries;
              }
              set
              {
                  _SelectedCountries = value;
                  RaisePropertyChanged("SelectedCountries");
              }
          }
    I've tried implement  the same solution as described there:
    http://www.telerik.com/community/forums/silverlight/listbox/radlistbox-selecteditems-mvvm.aspx
    but without any success, looks like only get is appear. : 
    <StackPanel Margin="0">
                        <telerikInput:RadListPicker
                            x:Name="CountryPicker"
                            ItemsSource="{Binding TitleCountries}"                       
                            Header="Country"
                            SelectionMode="Multiple"   
                            SelectedItem="{Binding SelectedCountries,Mode=TwoWay}"
                            OkButtonIconUri="/Toolkit.Content/ApplicationBar.Check.png"
                            CancelButtonIconUri="/Toolkit.Content/ApplicationBar.Cancel.png"
                            PopupHeader="">            
     
                      <i:Interaction.Triggers>
                            <i:EventTrigger EventName="SelectionChanged">
                                <cmd:EventToCommand Command="{Binding MethodChangedCommand}" CommandParameter="{Binding SelectedItems, ElementName=CountryPicker}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
    public class RadListPickerSelectedItemsBehavior : Behavior<RadListPicker>
    {
            private RadListPicker ListPicker
        {
            get
            {
                return AssociatedObject;
            }
        }
      
        public INotifyCollectionChanged SelectedItems
        {
            get { return (INotifyCollectionChanged)GetValue(SelectedItemsProperty); }
            set {
                SetValue(SelectedItemsProperty, value);
            }
        }
      
        public static readonly DependencyProperty SelectedItemsProperty =
            DependencyProperty.Register("SelectedItems",
            typeof(INotifyCollectionChanged),
            typeof(RadListPickerSelectedItemsBehavior),
            new PropertyMetadata(OnSelectedItemsPropertyChanged));
      
      
        private static void OnSelectedItemsPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
        {
            var collection = args.NewValue as INotifyCollectionChanged;
            if (collection != null)
            {
                collection.CollectionChanged += ((RadListPickerSelectedItemsBehavior)target).ContextSelectedItemsCollectionChanged;
            }
        }
      
        protected override void OnAttached()
        {
            base.OnAttached();
      
            ListPicker.SelectionChanged += ListBoxOnSelectionChanged;
        }
      
        private void ContextSelectedItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            UnsubscribeFromEvents();
      
            Transfer(SelectedItems as IList, ListPicker.SelectedItems);
      
            SubscribeToEvents();
        }
      
        private void ListBoxOnSelectionChanged(object sender, SelectionChangedEventArgs selectionChangedEventArgs)
        {
            UnsubscribeFromEvents();
      
            Transfer(ListPicker.SelectedItems, SelectedItems as IList);
      
            SubscribeToEvents();
        }
      
        private void SubscribeToEvents()
        {
            ListPicker.SelectionChanged += ListBoxOnSelectionChanged;
      
            if (SelectedItems != null)
            {
                SelectedItems.CollectionChanged += ContextSelectedItemsCollectionChanged;
            }
        }
      
        private void UnsubscribeFromEvents()
        {
            ListPicker.SelectionChanged -= ListBoxOnSelectionChanged;
      
            if (SelectedItems != null)
            {
                SelectedItems.CollectionChanged -= ContextSelectedItemsCollectionChanged;
            }
        }
      
        public static void Transfer(IList source, IList target)
        {
            if (source == null || target == null)
                return;
      
            target.Clear();
      
            foreach (var o in source)
            {
                target.Add(o);
            }
        }
    }
    could anybody say how to GET/SET selected items of the RadListPicker using MVVM pattern?
  2. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1511 posts

    Posted 10 Dec 2012 Link to this post

    Hello Vladimir,

    Currently, setting SelectedItems through binding is not supported. You will have to manually populate the collection. For example:

    Country c = new Country();
    c.Name = "Germany";
    this.TitleCountries.Add(c);
    this.CountryPicker.SelectedItems.Add(c);
     

    All the best,
    Kiril Stanoev
    the Telerik team
    Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
  3. DevCraft banner
Back to Top