Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
325
How to set initial Display Text?
posted

this link(http://ko.infragistics.com/community/blogs/devin_rader/archive/2011/06/01/using-behaviors-to-synchronize-selected-items-of-infragistics-silverlight-controls-to-a-viewmodel.aspx) shows how to synchronize multipleSelection from the UI side.

but if i initialize the SelectedCustomers property by adding two customer, run it, the UI side doesn't show the information.

now i can get the multipleSelection itmes' value by this following way,

1. define the SelectedItemsValues

        public static readonly DependencyProperty SelectedItemsValuesProperty = DependencyProperty.Register("SelectedItemsValues", typeof(List), typeof(XamComboEditorSelectedItemsBehavior), new PropertyMetadata(OnSelectedItemsPropertyChanged));
        public List SelectedItemsValues

2. in the  Combobox.SelectedItems.CollectionChanged event, foreach the items to get the value what i want. each item is ICityDimension type.

            UnsubscribeFromEvents();
            Transfer(Combobox.SelectedItems, SelectedItems as IList);
            List results= new List();
            foreach (object tmp in Combobox.SelectedItems)
            {
                convert.Add(((ICityDimension)val).Description);
            }
            SelectedItemsValues = convert;
            SubscribeToEvents();

anybody can tell me how to set the initial Display Text when the SelectedItems is from the DB side instead of user's UI selection,  or which property/evnet  related to Display the text? if possible, can give a demo(MVVM pattern)?thanks in advance.

my behavior class is as follows:::::

 public class XamComboEditorSelectedItemsBehavior : Behavior
    {
        private XamComboEditor Combobox
        {
            get
            {
                return AssociatedObject as XamComboEditor;
            }
        }

        public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(INotifyCollectionChanged), typeof(XamComboEditorSelectedItemsBehavior), new PropertyMetadata(OnSelectedItemsPropertyChanged));
        public INotifyCollectionChanged SelectedItems
        {
            get { return (INotifyCollectionChanged)GetValue(SelectedItemsProperty); }
            set { SetValue(SelectedItemsProperty, value); }
        }

        public static readonly DependencyProperty SelectedItemsValuesProperty = DependencyProperty.Register("SelectedItemsValues", typeof(List), typeof(XamComboEditorSelectedItemsBehavior), new PropertyMetadata(OnSelectedItemsPropertyChanged));
        public List SelectedItemsValues
        {
            get { return (List)this.GetValue(SelectedItemsValuesProperty); }
            set { this.SetValue(SelectedItemsValuesProperty, value); }
        }

        private static void OnSelectedItemsPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
        {
            var collection = args.NewValue as INotifyCollectionChanged;
            if (collection != null)
            {
                collection.CollectionChanged += ((XamComboEditorSelectedItemsBehavior)target).ContextSelectedItems_CollectionChanged;
            }
        }

        protected override void OnAttached()
        {
            base.OnAttached();

            Combobox.SelectedItems.CollectionChanged += new NotifyCollectionChangedEventHandler(SelectedItems_CollectionChanged);
            //Combobox.Loaded += new RoutedEventHandler(Combobox_Loaded);
      
        }

        void Combobox_Loaded(object sender, RoutedEventArgs e)
        {
            throw new NotImplementedException();
        }

        void ContextSelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            UnsubscribeFromEvents();

            Transfer(SelectedItems as IList, Combobox.SelectedItems);

            SubscribeToEvents();
        }

        void SelectedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            UnsubscribeFromEvents();
            Transfer(Combobox.SelectedItems, SelectedItems as IList);
            List convert = new List();
            foreach (object tmp in Combobox.SelectedItems)
            {
                convert.Add(((ICityDimension)tmp).Description);
            }
            SelectedItemsValues = convert;
            SubscribeToEvents();
        }

        private void SubscribeToEvents()
        {
            Combobox.SelectedItems.CollectionChanged += SelectedItems_CollectionChanged;

            if (SelectedItems != null)
            {
                SelectedItems.CollectionChanged += ContextSelectedItems_CollectionChanged;
            }
        }

        private void UnsubscribeFromEvents()
        {
            Combobox.SelectedItems.CollectionChanged -= SelectedItems_CollectionChanged;

            if (SelectedItems != null)
            {
                SelectedItems.CollectionChanged -= ContextSelectedItems_CollectionChanged;
            }
        }

        public static void Transfer(IList source, IList target)
        {
            if (source == null || target == null)
                return;

            target.Clear();

            foreach (var tmp in source)
            {
                target.Add(tmp);
            }
        }
    }