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);
}
}
}
}