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

Want to learn Comobox: Can you help me convert multi selecteditems to selecteditem

5 Answers 117 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
herb
Top achievements
Rank 1
Veteran
Iron
herb asked on 20 Dec 2020, 06:45 PM

I found a sample in your SDK "SelectedItemsBinding" part of your ComboBox.

Trying to understand the logic on using the ComboBox. This code seems very what I need but  I would like to use "SelectedItem" not "SelectedItems". So I working on converting your sample code to signal selection. I have managed to adjust the MainWindows.xaml and kind of understand the List collection. But can't convert this to a signal selection. Yes there are other more simple ComboBox samples but this one interests me.

I changed all the references to "SelectedItems" to "SelectedItem" where I get these errors in VS: "Error CS1503 Argument 1: cannot convert from 'object' to 'System.Collections.IList'"

Can you help me make this work?

using System.Collections;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Interactivity;
using Telerik.Windows.Controls;
 
namespace SelectedItemBinding
{
    public class SelectedItemBehavior : Behavior<RadComboBox>
    {
        private RadComboBox ComboBox
        {
            get
            {
                return this.AssociatedObject as RadComboBox;
            }
        }
 
        public INotifyCollectionChanged SelectedItem
        {
            get { return (INotifyCollectionChanged)this.GetValue(SelectedItemProperty); }
            set { this.SetValue(SelectedItemProperty, value); }
        }
 
        // Using a DependencyProperty as the backing store for SelectedItemProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.Register("SelectedItem", typeof(INotifyCollectionChanged), typeof(SelectedItemBehavior), new PropertyMetadata(OnSelectedItemPropertyChanged));
         
        private static void OnSelectedItemPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
        {
            var collection = args.NewValue as INotifyCollectionChanged;
            if (collection != null)
            {
                ((SelectedItemBehavior)target).UpdateTransfer(args.NewValue);
                collection.CollectionChanged += ((SelectedItemBehavior)target).ContextSelectedItem_CollectionChanged;
            }
        }
 
        private void UpdateTransfer(object items)
        {
            Transfer(items as IList, this.ComboBox.SelectedItem);
            this.ComboBox.SelectionChanged += this.ComboSelectionChanged;
        }
 
        private void ContextSelectedItem_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            this.UnsubscribeFromEvents();
            Transfer(SelectedItem as IList, this.ComboBox.SelectedItem);
            this.SubscribeToEvents();
        }
 
        private void ComboSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (this.ComboBox.ItemsSource != null)
            {
                this.UnsubscribeFromEvents();
                Transfer(this.ComboBox.SelectedItem, SelectedItem as IList);
                this.SubscribeToEvents();
            }
        }
 
        private void SubscribeToEvents()
        {
            this.ComboBox.SelectionChanged += this.ComboSelectionChanged;
            if (this.SelectedItem != null)
            {
                this.SelectedItem.CollectionChanged += this.ContextSelectedItem_CollectionChanged;
            }
        }
 
        private void UnsubscribeFromEvents()
        {
            this.ComboBox.SelectionChanged -= this.ComboSelectionChanged;
            if (this.SelectedItem != null)
            {
                this.SelectedItem.CollectionChanged -= this.ContextSelectedItem_CollectionChanged;
            }
        }
 
        public static void Transfer(IList source, IList target)
        {
            if (source == null || target == null)
                return;
 
            target.Clear();
            foreach (var o in source)
            {
                target.Add(o);
            }
        }
    }
}     

5 Answers, 1 is accepted

Sort by
0
herb
Top achievements
Rank 1
Veteran
Iron
answered on 20 Dec 2020, 06:50 PM

Sorry click too fast.

Lines 41, 48, 57 are where I don't know what the proper conditions are.

I figured I will not be calling the "Transfer" method on line 80 but I do need to make some action in the "OnSelectItemPropertyChanged" (line 29) so the INotifyCollectionChanged is passed the SelectedItem.

0
Martin Ivanov
Telerik team
answered on 22 Dec 2020, 10:40 AM

Hello Herb,

The purpose of the SelectedItemsBinding SDK example is to show you how to data bind the SelectedItems property of RadComboBox to a collection from the view model. The code in the example (more specifically the SelectedItemBehavior) is required because the SelectedItems property of RadComboBox is readonly and it cannot be data bound to the view model directly, like this:

<telerik:RadComboBox SelectedItems="{Binding MySelectedItems}" />

If you want to have only a single selection, you don't need the code from the example. Instead, you can simply data bind the property in your view model to the SelectedItems property of RadComboBox. For example:

 

public class MainViewModel : ViewModelBase
{
	private object selectedItem;
	public ObservableCollection<string> SelectedItem
	{
		get { return this.selectedItem; }
		set
		{
			if (this.selectedItem != value)
			{
				this.selectedItem = value;
				this.OnPropertyChanged(() => this.SelectedItem);
			}
		}
	}
}

<telerik:RadComboBox SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />

I hope this helps.

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
herb
Top achievements
Rank 1
Veteran
Iron
answered on 22 Dec 2020, 03:11 PM

Thank you Martin,

The example is a good working model for not only the RadComboBox it is good for the "INotifyCollectionChanged" which updates the "TextBlock Text="{Binding SelectedAgencies.Count}"". Other example fall short on helping with "real world" learning. I just thought if this code could be converted to use signal "SelctedItem" I could step through it to understand how to use this else where.

So if I could get help in this exercise it would be great.

0
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 25 Dec 2020, 12:53 PM

Hello Herb,

My name is Dinko and I'm stepping in for my colleague Martin, who is currently out of the office.

I am a little unsure of what you are trying to achieve here. As Martin mentioned in his reply, you don't need the SelectedItemBehavior custom class if you just want to bind the SelectedItem property only. I have prepared a sample project which demonstrates how you can bind the SelectedItem property. May I ask you to check the project and let me know what else you need to add to the project to achieve your scenario?

Regards,
Dinko
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
herb
Top achievements
Rank 1
Veteran
Iron
answered on 25 Dec 2020, 03:03 PM

Happy Holidays.

Thank you Dinko.

 

Tags
ComboBox
Asked by
herb
Top achievements
Rank 1
Veteran
Iron
Answers by
herb
Top achievements
Rank 1
Veteran
Iron
Martin Ivanov
Telerik team
Dinko | Tech Support Engineer
Telerik team
Share this question
or