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

RadListPicker SelectedItems MVVM

1 Answer 167 Views
ListPicker
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Vladimir
Top achievements
Rank 1
Vladimir asked on 07 Dec 2012, 09:54 AM
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?

1 Answer, 1 is accepted

Sort by
0
Kiril Stanoev
Telerik team
answered on 10 Dec 2012, 08:30 AM
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.
Tags
ListPicker
Asked by
Vladimir
Top achievements
Rank 1
Answers by
Kiril Stanoev
Telerik team
Share this question
or