Example #1
0
            void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                var listBox = sender as System.Windows.Controls.ListBox;

                //listBox.IsSynchronizedWithCurrentItem = true;
                if (listBox.SelectedItems.Count == 0)
                {
                    listBox.SelectedIndex = 0;
                }
                //消除PropertyChanged
                if (_previousSelectedItem != null)
                {
                    var notify = (INotifyPropertyChanged)_previousSelectedItem;
                    notify.PropertyChanged -= Item_PropertyChanged;
                    foreach (var item in _previousSubItems)
                    {
                        item.PropertyChanged -= SubItem_PropertyChanged;
                    }
                    _previousSubItems.Clear();
                }

                if (listBox.SelectedItems.Count > 1)    //>1时,开启多选监听
                {
                    var item = (INotifyPropertyChanged)listBox.SelectedItem;
                    _previousSelectedItem = item;
                    item.PropertyChanged += Item_PropertyChanged;
                    //子属性
                    _multiUpdateProperties = _listBox.GetValue(ItemsElementBehavior.MultiUpdatePropertyNamesProperty) as IList;
                    var deepProperties = _multiUpdateProperties.Cast <string>().Where(s => s.Contains('.'));
                    if (deepProperties.Count() > 0)
                    {
                        foreach (var property in deepProperties)
                        {
                            var array   = property.Split('.');
                            var index   = array.Length - 1;
                            var subItem = item.GetValueByIndex(property, index);
                            if (subItem is INotifyPropertyChanged)
                            {
                                var notify = subItem as INotifyPropertyChanged;
                                notify.PropertyChanged += SubItem_PropertyChanged;
                                _previousSubItems.Add(notify);
                            }
                        }
                    }
                }
                else
                {
                    _previousSelectedItem = null;
                }
            }
 public static void SetHasBindableSelectedItems(ListBox source, bool value)
 {
     SelectionChangedHandler Handler = (SelectionChangedHandler)source.GetValue(SelectionChangedHandlerProperty);
     if (value && Handler == null)
     {
         Handler = new SelectionChangedHandler(source);
         source.SetValue(SelectionChangedHandlerProperty, Handler);
     }
     else if (!value && Handler != null)
     { source.ClearValue(SelectionChangedHandlerProperty); }
 }
Example #3
0
        private static ListSelectCommandBehavior GetOrCreateBehavior(ListBox selector)
        {
            var behavior = selector.GetValue(SelectCommandBehaviorProperty) as ListSelectCommandBehavior;
            if (behavior == null)
            {
                behavior = new ListSelectCommandBehavior(selector);
                selector.SetValue(SelectCommandBehaviorProperty, behavior);
            }

            return behavior;
        }
Example #4
0
        private static SelectedItemsBehavior GetOrCreateBehavior(ListBox target, IList list)
        {
            var behavior = target.GetValue(SelectedItemsBehaviorProperty) as SelectedItemsBehavior;
            if (behavior == null)
            {
                behavior = new SelectedItemsBehavior(target, list);
                target.SetValue(SelectedItemsBehaviorProperty, behavior);
            }

            return behavior;
        }
        private static ListBoxSelectionChangedCommandBehavior GetOrCreateBehavior(ListBox ListBox)
        {
            ListBoxSelectionChangedCommandBehavior behavior =
            ListBox.GetValue(SelectCommandBehaviorProperty) as ListBoxSelectionChangedCommandBehavior;

              if (behavior == null)
              {
            behavior = new ListBoxSelectionChangedCommandBehavior(ListBox);
            ListBox.SetValue(SelectCommandBehaviorProperty, behavior);
              }
              return behavior;
        }
Example #6
0
 public static void SetAutoScroll(ListBox instance, bool value)
 {
     var oldHandler = (AutoScrollHandler)instance.GetValue(AutoScrollHandlerProperty);
     if (oldHandler != null)
     {
         oldHandler.Dispose();
         instance.SetValue(AutoScrollHandlerProperty, null);
     }
     instance.SetValue(AutoScrollProperty, value);
     if (value)
         instance.SetValue(AutoScrollHandlerProperty, new AutoScrollHandler(instance));
 }
Example #7
0
        public static void SetForceSingleSelectionModeItemIndex(System.Windows.Controls.ListBox source, int value)
        {
            if (source != null)
            {
                source.SelectionChanged += (sender, e) =>
                {
                    if (e.AddedItems.Count == 0)
                    {
                        return;
                    }
                    var selectedItem  = e.AddedItems[0];
                    var lb            = sender as System.Windows.Controls.ListBox;
                    var selectedIndex = lb.Items.IndexOf(selectedItem);

                    var itemIndexValue = (int)source.GetValue(ForceSingleSelectionModeItemIndexProperty);
                    var selectedItems  = (IList)source.GetValue(BindableSelectedItemsProperty);

                    if (selectedIndex == itemIndexValue)
                    {
                        foreach (var item in lb.Items)
                        {
                            if (item != selectedItem)
                            {
                                if (selectedItems.Contains(item))
                                {
                                    selectedItems.Remove(item);
                                }
                            }
                        }
                    }
                    else
                    {
                        selectedItems.Remove(lb.Items[itemIndexValue]);
                    }
                };

                source.SetValue(ForceSingleSelectionModeItemIndexProperty, value);
            }
        }
Example #8
0
        public static void SetHasBindableSelectedItems(System.Windows.Controls.ListBox source, bool value)
        {
            var handler = (SelectionChangedHandler)source.GetValue(SelectionChangedHandlerProperty);

            if (value && handler == null)
            {
                handler = new SelectionChangedHandler(source);
                source.SetValue(SelectionChangedHandlerProperty, handler);
            }
            else if (!value && handler != null)
            {
                source.ClearValue(SelectionChangedHandlerProperty);
            }
        }
Example #9
0
        public static void SetAutoScroll(System.Windows.Controls.ListBox instance, bool value)
        {
            AutoScrollHandler OldHandler = (AutoScrollHandler)instance.GetValue(AutoScrollHandlerProperty);

            if (OldHandler != null)
            {
                OldHandler.Dispose();
                instance.SetValue(AutoScrollHandlerProperty, null);
            }
            instance.SetValue(AutoScrollProperty, value);
            if (value)
            {
                instance.SetValue(AutoScrollHandlerProperty, new AutoScrollHandler(instance));
            }
        }
Example #10
0
 private static ListChangedHandler GetChangedHandler(ListBox obj)
 {
     return (ListChangedHandler)obj.GetValue(ChangedHandlerProperty);
 }
Example #11
0
 public static bool GetIsActivatable(ListBox obj)
 {
     return (bool)obj.GetValue(IsActivatableProperty);
 }
 public static IList GetItems(ListBox listBox)
 {
     return listBox.GetValue(ItemsProperty) as IList;
 }
Example #13
0
 public static bool GetIsListboxItemBringIntoView(ListBox listBox)
 {
     return (bool)listBox.GetValue(IsListboxItemBringIntoViewProperty);
 }
Example #14
0
 public static bool GetIsSelectedItemScrolledIntoView(ListBox listBox)
 {
     return (bool)listBox.GetValue(IsSelectedItemScrolledIntoViewProperty);
 }
Example #15
0
        private void DragStarted(ListBox listbox)
        {
            listbox.ClearValue(IsDownProperty); // SetValue to false would also work.

            // Add the bound item, available as DraggingElement, to a DataObject, however we see fit.
            Artist draggingElement = (Artist)listbox.GetValue(DraggingElementProperty);
            DataObject d = new DataObject(DataFormats.Dif, draggingElement);
            DragDropEffects effects = DragDrop.DoDragDrop(listbox, d, DragDropEffects.Copy);
            if ((effects & DragDropEffects.Move) != 0)
            {
                // Move rather than copy, so we should remove from bound list.
                DataList<Artist> collection = (DataList<Artist>)listbox.ItemsSource;
                collection.Remove(draggingElement);
            }
        }
Example #16
0
 /// <summary>
 /// Retrieves the <see cref="ICommand"/> attached to the <see cref="TextBox"/>.
 /// </summary>
 /// <param name="selector">TextBox containing the Command dependency property</param>
 /// <returns>The value of the command attached</returns>
 public static ICommand GetCommand(ListBox selector)
 {
     return selector.GetValue(CommandProperty) as ICommand;
 }
Example #17
0
 public static bool GetAutoScroll(ListBox instance)
 {
     return (bool)instance.GetValue(AutoScrollProperty);
 }
 public static bool GetIsAutoCenterItem(ListBox element)
 {
     return (bool)element.GetValue(IsAutoCenterItemProperty);
 }
 public static bool GetCenterSelectedItemOnLoad(ListBox ListBox)
 {
     return (bool)ListBox.GetValue(CenterSelectedItemOnLoadProperty);
 }
 public static bool GetIsBroughtIntoViewWhenInserted(ListBox listBox)
 {
     return (bool)listBox.GetValue(IsBroughtIntoViewWhenInsertedProperty);
 }
Example #21
0
 public static bool GetAutoScroll(System.Windows.Controls.ListBox instance)
 {
     return((bool)instance.GetValue(AutoScrollProperty));
 }
Example #22
0
        public void B_should_return_default_from_GetValue()
        {
            var listBox = new ListBox();

            listBox.GetValue(MyObjectWithDefaultValue.MyTextProperty).ToString().ShouldBe("default");
        }
 public static ICommand GetCommand(ListBox ListBox)
 {
     return (ListBox.GetValue(CommandProperty) as ICommand);
 }
 public static ListBoxMouseWheelScroller GetListBoxMouseWheelScroller(ListBox element) { return (ListBoxMouseWheelScroller)element.GetValue(ListBoxMouseWheelScrollerProperty); }
Example #25
0
 public static bool GetIsScrollIntoViewWhenSelected(System.Windows.Controls.ListBox obj)
 {
     return((bool)obj.GetValue(IsScrollIntoViewWhenSelectedProperty));
 }
Example #26
0
 public static bool GetSelectedItemsBinding(ListBox element)
 {
     return (bool)element.GetValue(SelectedItemsProperty);
 }
 public static object GetCommandParameter(ListBox ListBox)
 {
     return (ListBox.GetValue(CommandParameterProperty));
 }