From 21eb3cf8f8b424a7c7a3ba851958f4fda7d1ad3d Mon Sep 17 00:00:00 2001 From: COM8 Date: Sat, 3 Mar 2018 15:52:05 +0100 Subject: [PATCH] Closed #21 and fixed #20 --- .../Controls/MUCFieldControl.xaml.cs | 7 +- .../Controls/MultiSelectControl.xaml | 24 ++--- .../Controls/MultiSelectControl.xaml.cs | 87 +++++++------------ 3 files changed, 41 insertions(+), 77 deletions(-) diff --git a/UWP XMPP Client/Controls/MUCFieldControl.xaml.cs b/UWP XMPP Client/Controls/MUCFieldControl.xaml.cs index b84f84d5a..9e5cb2a49 100644 --- a/UWP XMPP Client/Controls/MUCFieldControl.xaml.cs +++ b/UWP XMPP Client/Controls/MUCFieldControl.xaml.cs @@ -82,7 +82,7 @@ private void showField() toggleField_tgls.Visibility = Visibility.Visible; break; - case MUCInfoFieldType.LIST_MULTI: + case MUCInfoFieldType.LIST_SINGLE: listField_cmbb.ItemsSource = Field.options; if (Field.selectedOptions.Count > 0) { @@ -91,11 +91,11 @@ private void showField() listField_cmbb.Visibility = Visibility.Visible; break; - case MUCInfoFieldType.LIST_SINGLE: + case MUCInfoFieldType.LIST_MULTI: label_tblck.Visibility = Visibility.Collapsed; listMulti_msc.header = Field.label ?? (Field.var ?? "No description / 'var' given!"); - listMulti_msc.setItems(new List(Field.options)); + listMulti_msc.itemSource = new List(Field.options); listMulti_msc.setSelectedItems(new List(Field.selectedOptions)); listMulti_msc.Visibility = Visibility.Visible; @@ -145,6 +145,7 @@ private void listField_cmbb_SelectionChanged(object sender, SelectionChangedEven { if (listField_cmbb.SelectedItem is MUCInfoOption) { + Field.selectedOptions.Clear(); Field.selectedOptions.Add(listField_cmbb.SelectedItem as MUCInfoOption); } } diff --git a/UWP XMPP Client/Controls/MultiSelectControl.xaml b/UWP XMPP Client/Controls/MultiSelectControl.xaml index 154363c02..0ee0f510c 100644 --- a/UWP XMPP Client/Controls/MultiSelectControl.xaml +++ b/UWP XMPP Client/Controls/MultiSelectControl.xaml @@ -5,27 +5,15 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls" - xmlns:datatemplates="using:UWP_XMPP_Client.DataTemplates" - mc:Ignorable="d"> - - - - - - + mc:Ignorable="d" + Loaded="UserControl_Loaded"> + ItemsSource="{x:Bind itemSource, Mode=TwoWay}" + MaxHeight="{x:Bind MaxHeight, Mode=OneWay}" + SelectionMode="Multiple" + SelectionChanged="items_listv_SelectionChanged"/> diff --git a/UWP XMPP Client/Controls/MultiSelectControl.xaml.cs b/UWP XMPP Client/Controls/MultiSelectControl.xaml.cs index 152489c2c..216730574 100644 --- a/UWP XMPP Client/Controls/MultiSelectControl.xaml.cs +++ b/UWP XMPP Client/Controls/MultiSelectControl.xaml.cs @@ -1,7 +1,5 @@ using System.Collections.Generic; -using UWP_XMPP_Client.Classes; using UWP_XMPP_Client.Classes.Events; -using UWP_XMPP_Client.DataTemplates; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; @@ -25,14 +23,18 @@ public int maxContentHeight } public static readonly DependencyProperty maxContentHeightProperty = DependencyProperty.Register("maxContentHeight", typeof(int), typeof(MultiSelectControl), null); - private CustomObservableCollection items; + public List itemSource + { + get { return (List)GetValue(itemSourceProperty); } + set { SetValue(itemSourceProperty, value); } + } + public static readonly DependencyProperty itemSourceProperty = DependencyProperty.Register("itemSource", typeof(List), typeof(MultiSelectControl), null); + private List selectedItems; public delegate void SelectionChangedMultiEventHandler(MultiSelectControl sender, SelectionChangedMultiEventArgs args); public event SelectionChangedMultiEventHandler SelectionChanged; - private static object selectedItemsLock = new object(); - #endregion //--------------------------------------------------------Constructor:----------------------------------------------------------------\\ #region --Constructors-- @@ -46,7 +48,6 @@ public MultiSelectControl() { this.MaxHeight = 200; this.selectedItems = new List(); - this.items = new CustomObservableCollection(); this.InitializeComponent(); } @@ -60,26 +61,8 @@ public IList getSelectedItems() public void setSelectedItems(List list) { - lock (selectedItemsLock) - { - selectedItems.Clear(); - selectedItems.AddRange(list); - } - } - - public void setItems(List list) - { - items.Clear(); - List templateList = new List(); - for (int i = 0; i < list.Count; i++) - { - templateList.Add(new MultiSelectTemplate() - { - isSelected = false, - item = list[i] - }); - } - items.AddRange(templateList); + selectedItems.Clear(); + selectedItems.AddRange(list); } #endregion @@ -90,36 +73,12 @@ public void setItems(List list) #endregion #region --Misc Methods (Private)-- - private void updateSelectedItems() - { - lock (selectedItemsLock) - { - selectedItems.Clear(); - for (int i = 0; i < items.Count; i++) - { - if (items[i].isSelected) - { - selectedItems.Add(items[i].item); - } - } - } - SelectionChanged?.Invoke(this, new SelectionChangedMultiEventArgs(selectedItems)); - } - private void showSelectedItems() { - lock (selectedItemsLock) + items_listv.SelectedItems.Clear(); + for (int i = 0; i < selectedItems.Count; i++) { - for (int i = 0; i < selectedItems.Count; i++) - { - for (int e = 0; e < items.Count; e++) - { - if (Equals(selectedItems[i], items[e].item)) - { - items[e].isSelected = true; - } - } - } + items_listv.SelectedItems.Add(selectedItems[i]); } } @@ -131,14 +90,30 @@ private void showSelectedItems() #endregion //--------------------------------------------------------Events:---------------------------------------------------------------------\\ #region --Events-- - private void items_listv_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args) + private void UserControl_Loaded(object sender, RoutedEventArgs e) { showSelectedItems(); } - private void CheckBox_CheckedChanged(object sender, RoutedEventArgs e) + private void items_listv_SelectionChanged(object sender, SelectionChangedEventArgs e) { - updateSelectedItems(); + // Remove items: + for (int i = 0; i < e.RemovedItems.Count; i++) + { + selectedItems.Remove(e.RemovedItems[i]); + } + + // Add items: + for (int i = 0; i < e.AddedItems.Count; i++) + { + if (!selectedItems.Contains(e.AddedItems[i])) + { + selectedItems.Add(e.AddedItems[i]); + } + } + + // Trigger the event: + SelectionChanged?.Invoke(this, new SelectionChangedMultiEventArgs(selectedItems)); } #endregion