GetValue of SelectedItems in attached property doesnt get SelectedItems why?
2 Answers 16 Views
Kinglouy
Top achievements
Rank 1
Iron
Kinglouy asked on 05 May 2021, 07:48 AM | edited on 05 May 2021, 07:50 AM

 

Hello,

I have a problem to get the SelectedItems from my radcombobox. I have created an attached property "SelectedItemsHelper" to use the "SelectedItems" property. As a template for the attached property, I 've used this blog post.

My SelectedItemsHelper

using System.Collections;
using System.Windows;
using Telerik.Windows.Controls;

namespace WpfAutoQuery
{
    public class SelectedItemsHelper
    {
        
        public static readonly DependencyProperty SelectedItemsProperty =
            DependencyProperty.RegisterAttached("SelectedItems", typeof(IList), typeof(SelectedItemsHelper), new FrameworkPropertyMetadata((IList)null, new PropertyChangedCallback(OnSelectedItemsChanged)));
              
        public static IList GetSelectedItems(DependencyObject d)
        {
            return (IList)d.GetValue(SelectedItemsProperty);
        }

        public static void SetSelectedItems(DependencyObject d, IList value)
        {
            d.SetValue(SelectedItemsProperty, value);
        }

        private static void OnSelectedItemsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var comboBox = sender as RadComboBox;
            if (comboBox != null)
            {                
                IList selectedItems = GetSelectedItems(comboBox);
                if(selectedItems != null)
                {
                    comboBox.SelectedItems.Clear();
                    foreach (var item in selectedItems)
                    {
                        comboBox.SelectedItems.Add(item);
                    }
                }
            }                               
        }
    }
}

My RadComboBox:

<telerik:RadComboBox Name="rcbSection" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2" Margin="10" IsEditable="False"
                                       ItemsSource="{Binding Sections}" DisplayMemberPath="Line1" AllowMultipleSelection="True"  
                                       local:SelectedItemsHelper.SelectedItems="{Binding SelectedSections}">                                              
</telerik:RadComboBox>

My "SelectedSections" property in ViewModel

public ObservableCollection<Section> SelectedSections
            {
                get { return GetPropertyValue<ObservableCollection<Section>>(); }
                set { SetPropertyValue(value); }
            }

I have added a jpg, where you can see that i dont get my selected values... Does someone can help me? I dont know what i do wrong...

 

2 Answers, 1 is accepted

Sort by
0
Kinglouy
Top achievements
Rank 1
Iron
answered on 06 May 2021, 12:58 PM

I have found a solution. What i changed so far:

ViewModel 

    private ObservableCollection<Section> _selectedSections = new ObservableCollection<Section>();
    public ObservableCollection<Section> SelectedSections
    {
        get { return _selectedSections; }
    } 

    var selectedSectionList = _selectedSections;


SelectedItemsHelper

private static void OnSelectedItemsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var comboBox = sender as RadComboBox;
            IList coll = e.NewValue as IList;

            if (coll is INotifyCollectionChanged)
            {
                (coll as INotifyCollectionChanged)
                    .CollectionChanged += (s, e3) =>
                    {
                        if (null != e3.OldItems)
                            foreach (var item in e3.OldItems)
                                comboBox.SelectedItems.Remove(item);
                        if (null != e3.NewItems)
                            foreach (var item in e3.NewItems)
                                comboBox.SelectedItems.Add(item);
                    };
            }
            if (null != coll)
            {
                if (coll.Count > 0)
                {
                    comboBox.SelectedItems.Clear();
                    foreach (var item in coll)
                        comboBox.SelectedItems.Add(item);
                }

                comboBox.SelectionChanged += (s, e2) =>
                {
                    if (null != e2.RemovedItems)
                        foreach (var item in e2.RemovedItems)
                            coll.Remove(item);
                    if (null != e2.AddedItems)
                        foreach (var item in e2.AddedItems)
                            coll.Add(item);
                };
            }
        }

0
Vladimir Stoyanov
Telerik team
answered on 07 May 2021, 07:20 AM

Hello,

I am glad to hear that you have found an approach that is suitable for your scenario. 

Note, that you can check out the following SDK example: SelectedItemsBinding for a runnable project demonstrating how the desired functionality can be achieved. 

Regards,
Vladimir Stoyanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Asked by
Kinglouy
Top achievements
Rank 1
Iron
Answers by
Kinglouy
Top achievements
Rank 1
Iron
Vladimir Stoyanov
Telerik team
Share this question
or