Added ThreeState IsChecked logic to ItemFilterBlockGroup

This commit is contained in:
Ben 2015-06-11 21:01:33 +01:00
parent 6d9edbda98
commit 3ee7e1cc2a
4 changed files with 96 additions and 6 deletions

@ -1,14 +1,21 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using Filtration.Annotations;
namespace Filtration.Models
{
internal class ItemFilterBlockGroup
internal class ItemFilterBlockGroup : INotifyPropertyChanged
{
private bool? _isChecked;
private bool _reentrancyCheck;
public ItemFilterBlockGroup(string groupName, ItemFilterBlockGroup parent)
{
GroupName = groupName;
ParentGroup = parent;
ChildGroups = new List<ItemFilterBlockGroup>();
ChildGroups = new ObservableCollection<ItemFilterBlockGroup>();
}
public override string ToString()
@ -32,6 +39,79 @@ namespace Filtration.Models
public string GroupName { get; private set; }
public ItemFilterBlockGroup ParentGroup { get; private set; }
public List<ItemFilterBlockGroup> ChildGroups { get; private set; }
public ObservableCollection<ItemFilterBlockGroup> ChildGroups { get; private set; }
public bool? IsChecked
{
get
{
return _isChecked;
}
set
{
if (_isChecked != value)
{
if (_reentrancyCheck)
{
return;
}
_reentrancyCheck = true;
_isChecked = value;
UpdateCheckState();
OnPropertyChanged();
_reentrancyCheck = false;
}
}
}
private void UpdateCheckState()
{
// update all children:
if (ChildGroups.Count != 0)
{
UpdateChildrenCheckState();
}
// update parent item
if (ParentGroup != null)
{
var parentIsChecked = ParentGroup.DetermineCheckState();
ParentGroup.IsChecked = parentIsChecked;
}
}
private void UpdateChildrenCheckState()
{
foreach (var childGroup in ChildGroups.Where(c => IsChecked != null))
{
childGroup.IsChecked = IsChecked;
}
}
private bool? DetermineCheckState()
{
var allChildrenChecked = ChildGroups.Count(x => x.IsChecked == true) == ChildGroups.Count;
if (allChildrenChecked)
{
return true;
}
var allChildrenUnchecked = ChildGroups.Count(x => x.IsChecked == false) == ChildGroups.Count;
if (allChildrenUnchecked)
{
return false;
}
return null;
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}

@ -10,7 +10,7 @@
xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock"
xmlns:converters="clr-namespace:Filtration.Converters"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=viewModels:AvalonDockWorkspaceViewModel}"
d:DataContext="{d:DesignInstance d:Type=viewModels:AvalonDockWorkspaceViewModel}"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

@ -0,0 +1,10 @@
namespace Filtration.Views.AvalonDock
{
public partial class AvalonDockWorkspaceView
{
public AvalonDockWorkspaceView()
{
InitializeComponent();
}
}
}

@ -16,7 +16,7 @@
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type models:ItemFilterBlockGroup}" ItemsSource="{Binding ChildGroups}">
<WrapPanel>
<CheckBox IsThreeState="True" />
<CheckBox IsThreeState="True" IsChecked="{Binding IsChecked}" Click="BlockGroupCheckBox_Clicked" />
<TextBlock Text="{Binding GroupName}" />
</WrapPanel>
</HierarchicalDataTemplate>