This repository has been archived by the owner on Jun 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CommandContainer.cs
120 lines (105 loc) · 3.56 KB
/
CommandContainer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace SupportTool
{
class CommandContainer
{
private Config Config;
private Panel ParentUiElement;
private List<Tuple<CommandInterface, CheckBox>> CommandTuple = new List<Tuple<CommandInterface, CheckBox>>();
/// <summary>
/// All Commands attached, regardless of CheckBox.
/// </summary>
public List<CommandInterface> Commands
{
get
{
return CommandTuple.Select(item => item.Item1).ToList();
}
private set { }
}
/// <summary>
/// All CheckBoxes created for the Commands.
/// </summary>
public List<CheckBox> CheckBoxes
{
get
{
return CommandTuple.Select(item => item.Item2).Where(checkbox => null != checkbox).ToList();
}
private set { }
}
public CommandContainer(Config config, Panel parentUiElement)
{
Config = config;
ParentUiElement = parentUiElement;
}
/// <summary>
/// Enables all checkboxes.
/// </summary>
public void Enable()
{
foreach (CheckBox checkBox in CheckBoxes)
{
checkBox.IsEnabled = true;
}
}
/// <summary>
/// Disables all checkboxes
/// </summary>
public void Disable()
{
foreach (CheckBox checkBox in CheckBoxes)
{
checkBox.IsEnabled = false;
}
}
/// <summary>
/// Adds a command to the set.
///
/// Whenever the CommandCheckBoxInterface is implemented, a checkbox
/// will be added to the parent UI element.
/// </summary>
/// <param name="command"></param>
public void Add(CommandInterface command)
{
if (false == command is CommandCheckBoxInterface)
{
// no checkbox has to be created and kept track of, return early
CommandTuple.Add(Tuple.Create<CommandInterface, CheckBox>(command, null));
return;
}
CommandCheckBoxInterface elementAware = (CommandCheckBoxInterface)command;
CheckBox uiElement = new CheckBox()
{
Margin = new Thickness(3, 3, 0, 0),
Content = elementAware.Text
};
if (null != elementAware.ToolTip)
{
uiElement.ToolTip = new ToolTip()
{
Content = new TextBlock()
{
Text = elementAware.ToolTip,
MaxWidth = 300,
TextWrapping = TextWrapping.Wrap,
}
};
}
// creates a 2 way binding between the config values and the element
Binding binding = new Binding();
binding.Source = Config;
binding.Path = new PropertyPath(elementAware.ConfigPropertyPath);
binding.Mode = BindingMode.TwoWay;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(uiElement, CheckBox.IsCheckedProperty, binding);
ParentUiElement.Children.Add(uiElement);
CommandTuple.Add(Tuple.Create<CommandInterface, CheckBox>(command, uiElement));
}
}
}