Files
BeWoPlaner/BeWo/View/Controls/TextModuleTreeViewControl.xaml.cs

134 lines
3.6 KiB
C#
Raw Normal View History

2017-09-26 12:54:03 +02:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using BeWo.Annotations;
using BeWo.ViewModel;
using BS.Shared.Core;
using BS.Shared.Extensions;
namespace BeWo.View.Controls
{
public partial class TextModuleTreeViewControl : INotifyPropertyChanged
{
public event EventHandler<EventArgs<TextModuleVM>> TextModuleSelected;
public TextModuleTreeViewControl()
{
InitializeComponent();
DataContext = this;
}
public long? ServiceCategoryOid { get; set; }
private List<TextModuleVM> allTextModules = new List<TextModuleVM>();
public BindingList<TextModuleVM> FilteredList { get; set; }
private void OpenPopUpBtnClick(object sender, RoutedEventArgs e)
{
VMFactory.CreateTextModuleListVMAsync(
textModules => this.Dispatch(delegate
{
TreeViewPopup.Placement = PlacementMode.RelativePoint;
TreeViewPopup.HorizontalOffset = 0;
TreeViewPopup.IsOpen = true;
allTextModules = textModules.VMList.ToList();
if(ServiceCategoryOid != null)
{
FilteredList = new BindingList<TextModuleVM>();
var preFilteredTextModules = textModules.VMList.Where(w => w.ServiceCategory != null && w.ServiceCategory.ServiceCategoryOid == ServiceCategoryOid || w.ServiceCategory == null && !w.IsParent).ToList();
FilteredList.AddRange(preFilteredTextModules);
SearchTextModulesRecursively(preFilteredTextModules);
}
else
{
FilteredList = new BindingList<TextModuleVM>(textModules.VMList);
}
allTextModules = FilteredList.ToList();
OnPropertyChanged(nameof(FilteredList));
}));
}
private void TreeListControl_OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var selectedTextModule = (TextModuleVM) TreeListControl.SelectedItem;
if(selectedTextModule.IsParent)
{
2017-11-17 22:24:00 +01:00
var nodes = TreeListControl.GetSelectedNodes();
if (nodes != null)
{
foreach (var treeListNode in nodes)
{
treeListNode.IsExpanded = true;
}
}
return;
2017-09-26 12:54:03 +02:00
}
TreeViewPopup.IsOpen = false;
TextModuleSelected?.Invoke(this, new EventArgs<TextModuleVM>(selectedTextModule));
}
private void SearchTextBox_OnTextChanged(object sender, TextChangedEventArgs e)
{
var searchText = ((TextBox) sender).Text.Trim();
FilteredList.Clear();
if(searchText.IsNullOrEmpty())
{
FilteredList = new BindingList<TextModuleVM>(allTextModules.ToList());
}
else
{
var matchingChildren = allTextModules.Where(w => w.Name.Contains(searchText) || w.Text != null && w.Text.Contains(searchText)).ToList();
FilteredList.AddRange(matchingChildren);
SearchTextModulesRecursively(matchingChildren);
}
OnPropertyChanged(nameof(FilteredList));
TreeListControl.RefreshData();
}
private void SearchTextModulesRecursively(IReadOnlyCollection<TextModuleVM> pChildren)
{
if(pChildren.Count == 0)
{
return;
}
foreach(var child in pChildren)
{
var parents = allTextModules.Where(w => Equals(child.Parent, w) && !FilteredList.Contains(w)).ToList();
FilteredList.AddRange(parents);
SearchTextModulesRecursively(parents);
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}