Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
Closed #21 and fixed #20
Browse files Browse the repository at this point in the history
  • Loading branch information
COM8 committed Mar 3, 2018
1 parent 44f1afe commit 21eb3cf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 77 deletions.
7 changes: 4 additions & 3 deletions UWP XMPP Client/Controls/MUCFieldControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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<object>(Field.options));
listMulti_msc.itemSource = new List<object>(Field.options);
listMulti_msc.setSelectedItems(new List<object>(Field.selectedOptions));

listMulti_msc.Visibility = Visibility.Visible;
Expand Down Expand Up @@ -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);
}
}
Expand Down
24 changes: 6 additions & 18 deletions UWP XMPP Client/Controls/MultiSelectControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<UserControl.Resources>
<DataTemplate x:Key="listItem" x:DataType="datatemplates:MultiSelectTemplate">
<CheckBox Grid.Column="0"
IsChecked="{x:Bind isSelected, Mode=TwoWay}"
Content="{x:Bind item.ToString()}"
Margin="10"
Checked="CheckBox_CheckedChanged"
Unchecked="CheckBox_CheckedChanged"/>
</DataTemplate>
</UserControl.Resources>
mc:Ignorable="d"
Loaded="UserControl_Loaded">

<controls:Expander Header="{x:Bind header}"
HorizontalContentAlignment="Stretch">
<ListView x:Name="items_listv"
ItemsSource="{x:Bind items, Mode=TwoWay}"
MaxHeight="{x:Bind MaxHeight}"
ItemTemplate="{StaticResource listItem}"
SelectionMode="None"
DataContextChanged="items_listv_DataContextChanged"/>
ItemsSource="{x:Bind itemSource, Mode=TwoWay}"
MaxHeight="{x:Bind MaxHeight, Mode=OneWay}"
SelectionMode="Multiple"
SelectionChanged="items_listv_SelectionChanged"/>
</controls:Expander>
</UserControl>
87 changes: 31 additions & 56 deletions UWP XMPP Client/Controls/MultiSelectControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -25,14 +23,18 @@ public int maxContentHeight
}
public static readonly DependencyProperty maxContentHeightProperty = DependencyProperty.Register("maxContentHeight", typeof(int), typeof(MultiSelectControl), null);

private CustomObservableCollection<MultiSelectTemplate> items;
public List<object> itemSource
{
get { return (List<object>)GetValue(itemSourceProperty); }
set { SetValue(itemSourceProperty, value); }
}
public static readonly DependencyProperty itemSourceProperty = DependencyProperty.Register("itemSource", typeof(List<object>), typeof(MultiSelectControl), null);

private List<object> selectedItems;

public delegate void SelectionChangedMultiEventHandler(MultiSelectControl sender, SelectionChangedMultiEventArgs args);
public event SelectionChangedMultiEventHandler SelectionChanged;

private static object selectedItemsLock = new object();

#endregion
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\
#region --Constructors--
Expand All @@ -46,7 +48,6 @@ public MultiSelectControl()
{
this.MaxHeight = 200;
this.selectedItems = new List<object>();
this.items = new CustomObservableCollection<MultiSelectTemplate>();
this.InitializeComponent();
}

Expand All @@ -60,26 +61,8 @@ public IList<object> getSelectedItems()

public void setSelectedItems(List<object> list)
{
lock (selectedItemsLock)
{
selectedItems.Clear();
selectedItems.AddRange(list);
}
}

public void setItems(List<object> list)
{
items.Clear();
List<MultiSelectTemplate> templateList = new List<MultiSelectTemplate>();
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
Expand All @@ -90,36 +73,12 @@ public void setItems(List<object> 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]);
}
}

Expand All @@ -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
Expand Down

0 comments on commit 21eb3cf

Please sign in to comment.