From 463f793ac786d1b17f74791af1722ad9e603d8ca Mon Sep 17 00:00:00 2001 From: ermau Date: Thu, 5 Apr 2012 16:14:59 -0400 Subject: [PATCH] [WPF] Simplified ListBox/ListView selection models Really overthought this to begin with. The former solution should perform better, but it was proving difficult to debug for a negligible gain. A better design for XWTs access of selected items can just as easily fix any performance issues. --- Xwt.WPF/Xwt.WPFBackend/ExListBox.cs | 97 ------------------- Xwt.WPF/Xwt.WPFBackend/ExListView.cs | 109 ---------------------- Xwt.WPF/Xwt.WPFBackend/ListBoxBackend.cs | 17 +++- Xwt.WPF/Xwt.WPFBackend/ListViewBackend.cs | 14 ++- 4 files changed, 23 insertions(+), 214 deletions(-) diff --git a/Xwt.WPF/Xwt.WPFBackend/ExListBox.cs b/Xwt.WPF/Xwt.WPFBackend/ExListBox.cs index 967ce70b..ee511d3a 100644 --- a/Xwt.WPF/Xwt.WPFBackend/ExListBox.cs +++ b/Xwt.WPF/Xwt.WPFBackend/ExListBox.cs @@ -37,108 +37,11 @@ namespace Xwt.WPFBackend public class ExListBox : SWC.ListBox, IWpfWidget { - public ExListBox() - { - SelectedIndexes = new ObservableCollection (); - } - public WidgetBackend Backend { get; set; } - public static readonly DependencyProperty SelectedIndexesProperty = DependencyProperty.Register ( - "SelectedIndexes", - typeof (ICollection), typeof (ExListBox), - new UIPropertyMetadata (OnSelectedIndexesPropertyChanged)); - - public ICollection SelectedIndexes - { - get { return (ICollection) GetValue (SelectedIndexesProperty); } - set { SetValue (SelectedIndexesProperty, value); } - } - - protected override void OnSelectionChanged (SelectionChangedEventArgs e) - { - if (this.changingSelection) { - base.OnSelectionChanged (e); - return; - } - - this.changingSelection = true; - if (e.AddedItems != null) { - foreach (object item in e.AddedItems) { - SelectedIndexes.Add (Items.IndexOf (item)); - } - } - - if (e.RemovedItems != null) { - foreach (object item in e.RemovedItems) { - SelectedIndexes.Remove (Items.IndexOf (item)); - } - } - - this.changingSelection = false; - base.OnSelectionChanged (e); - } - - protected virtual void OnSelectedIndexesChanged (DependencyPropertyChangedEventArgs e) - { - var oldNotifying = e.OldValue as INotifyCollectionChanged; - if (oldNotifying != null) - oldNotifying.CollectionChanged -= SelectedIndexesChanged; - - var newNotifying = e.NewValue as INotifyCollectionChanged; - if (newNotifying != null) - newNotifying.CollectionChanged += SelectedIndexesChanged; - } - - private bool changingSelection; - private void SelectedIndexesChanged (object sender, NotifyCollectionChangedEventArgs e) - { - if (this.changingSelection) - return; - - this.changingSelection = true; - - if (SelectionMode == SWC.SelectionMode.Single) { - SelectedItem = null; - if (e.NewItems != null && e.NewItems.Count > 0) - SelectedItem = Items [(int) e.NewItems [0]]; - - this.changingSelection = false; - return; - } - - if (e.Action == NotifyCollectionChangedAction.Reset) { - SelectedItems.Clear(); - foreach (int index in SelectedIndexes) - { - SelectedItems.Add (Items[index]); - if (SelectionMode == SWC.SelectionMode.Single) - break; - } - } else { - if (e.NewItems != null) { - foreach (int index in e.NewItems) - SelectedItems.Add (Items[index]); - } - - if (e.OldItems != null) { - foreach (int index in e.OldItems) - SelectedItems.Remove (Items [index]); - } - } - - this.changingSelection = false; - } - - private static void OnSelectedIndexesPropertyChanged (DependencyObject dobj, DependencyPropertyChangedEventArgs e) - { - var listbox = (ExListBox) dobj; - listbox.OnSelectedIndexesChanged (e); - } - protected override System.Windows.Size MeasureOverride (System.Windows.Size constraint) { var s = base.MeasureOverride (constraint); diff --git a/Xwt.WPF/Xwt.WPFBackend/ExListView.cs b/Xwt.WPF/Xwt.WPFBackend/ExListView.cs index a0529950..98e786fe 100644 --- a/Xwt.WPF/Xwt.WPFBackend/ExListView.cs +++ b/Xwt.WPF/Xwt.WPFBackend/ExListView.cs @@ -24,13 +24,6 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Collections.Specialized; -using System.ComponentModel; -using System.Linq; -using System.Text; using System.Windows; using System.Windows.Controls; using SWC = System.Windows.Controls; @@ -40,11 +33,6 @@ namespace Xwt.WPFBackend public class ExListView : SWC.ListView, IWpfWidget { - public ExListView() - { - SelectedIndexes = new ObservableCollection (); - } - public WidgetBackend Backend { get; set; } protected override System.Windows.Size MeasureOverride (System.Windows.Size constraint) @@ -58,102 +46,5 @@ namespace Xwt.WPFBackend s = Backend.MeasureOverride (constraint, s); return s; } - - protected override System.Windows.Size ArrangeOverride (System.Windows.Size arrangeBounds) - { - return base.ArrangeOverride (arrangeBounds); - } - - public static readonly DependencyProperty SelectedIndexesProperty = DependencyProperty.Register ( - "SelectedIndexes", - typeof (ICollection), typeof (ExListView), - new UIPropertyMetadata (OnSelectedIndexesPropertyChanged)); - - public ICollection SelectedIndexes - { - get { return (ICollection) GetValue (SelectedIndexesProperty); } - set { SetValue (SelectedIndexesProperty, value); } - } - - protected override void OnSelectionChanged (SelectionChangedEventArgs e) - { - if (this.changingSelection) { - base.OnSelectionChanged (e); - return; - } - - this.changingSelection = true; - if (e.AddedItems != null) { - foreach (object item in e.AddedItems) { - SelectedIndexes.Add (Items.IndexOf (item)); - } - } - - if (e.RemovedItems != null) { - foreach (object item in e.RemovedItems) { - SelectedIndexes.Remove (Items.IndexOf (item)); - } - } - - this.changingSelection = false; - base.OnSelectionChanged (e); - } - - protected virtual void OnSelectedIndexesChanged (DependencyPropertyChangedEventArgs e) - { - var oldNotifying = e.OldValue as INotifyCollectionChanged; - if (oldNotifying != null) - oldNotifying.CollectionChanged -= SelectedIndexesChanged; - - var newNotifying = e.NewValue as INotifyCollectionChanged; - if (newNotifying != null) - newNotifying.CollectionChanged += SelectedIndexesChanged; - } - - private bool changingSelection; - private void SelectedIndexesChanged (object sender, NotifyCollectionChangedEventArgs e) - { - if (this.changingSelection) - return; - - this.changingSelection = true; - - if (SelectionMode == SWC.SelectionMode.Single) { - SelectedItem = null; - if (e.NewItems != null && e.NewItems.Count > 0) - SelectedItem = Items [(int) e.NewItems [0]]; - - this.changingSelection = false; - return; - } - - if (e.Action == NotifyCollectionChangedAction.Reset) { - SelectedItems.Clear(); - foreach (int index in SelectedIndexes) - { - SelectedItems.Add (Items[index]); - if (SelectionMode == SWC.SelectionMode.Single) - break; - } - } else { - if (e.NewItems != null) { - foreach (int index in e.NewItems) - SelectedItems.Add (Items[index]); - } - - if (e.OldItems != null) { - foreach (int index in e.OldItems) - SelectedItems.Remove (Items [index]); - } - } - - this.changingSelection = false; - } - - private static void OnSelectedIndexesPropertyChanged (DependencyObject dobj, DependencyPropertyChangedEventArgs e) - { - var view = (ExListView) dobj; - view.OnSelectedIndexesChanged (e); - } } } \ No newline at end of file diff --git a/Xwt.WPF/Xwt.WPFBackend/ListBoxBackend.cs b/Xwt.WPF/Xwt.WPFBackend/ListBoxBackend.cs index 92f75a42..2a7397af 100644 --- a/Xwt.WPF/Xwt.WPFBackend/ListBoxBackend.cs +++ b/Xwt.WPF/Xwt.WPFBackend/ListBoxBackend.cs @@ -91,19 +91,26 @@ namespace Xwt.WPFBackend ListBox.UnselectAll(); } - public int[] SelectedRows - { - get { return ListBox.SelectedIndexes.ToArray (); } + public int[] SelectedRows { + get { return ListBox.SelectedItems.Cast().Select (ListBox.Items.IndexOf).ToArray(); } } public void SelectRow (int pos) { - ListBox.SelectedIndexes.Add (pos); + object item = ListBox.Items [pos]; + if (ListBox.SelectionMode == System.Windows.Controls.SelectionMode.Single) + ListBox.SelectedItem = item; + else + ListBox.SelectedItems.Add (item); } public void UnselectRow (int pos) { - ListBox.SelectedIndexes.Remove (pos); + object item = ListBox.Items [pos]; + if (ListBox.SelectionMode == System.Windows.Controls.SelectionMode.Extended) + ListBox.SelectedItems.Remove (item); + else if (ListBox.SelectedItem == item) + ListBox.SelectedItem = null; } public override void EnableEvent (object eventId) diff --git a/Xwt.WPF/Xwt.WPFBackend/ListViewBackend.cs b/Xwt.WPF/Xwt.WPFBackend/ListViewBackend.cs index 3f797e59..aad8023d 100644 --- a/Xwt.WPF/Xwt.WPFBackend/ListViewBackend.cs +++ b/Xwt.WPF/Xwt.WPFBackend/ListViewBackend.cs @@ -85,7 +85,7 @@ namespace Xwt.WPFBackend } public int[] SelectedRows { - get { return ListView.SelectedIndexes.ToArray (); } + get { return ListView.SelectedItems.Cast().Select (ListView.Items.IndexOf).ToArray (); } } public object AddColumn (ListViewColumn col) @@ -151,12 +151,20 @@ namespace Xwt.WPFBackend public void SelectRow (int pos) { - ListView.SelectedIndexes.Add (pos); + object item = ListView.Items [pos]; + if (ListView.SelectionMode == System.Windows.Controls.SelectionMode.Single) + ListView.SelectedItem = item; + else + ListView.SelectedItems.Add (item); } public void UnselectRow (int pos) { - ListView.SelectedIndexes.Remove (pos); + object item = ListView.Items [pos]; + if (ListView.SelectionMode == System.Windows.Controls.SelectionMode.Extended) + ListView.SelectedItems.Remove (item); + else if (ListView.SelectedItem == item) + ListView.SelectedItem = null; } public override void EnableEvent(object eventId)