This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 770
/
AbstractOptionPanel.cs
125 lines (99 loc) · 3.21 KB
/
AbstractOptionPanel.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
121
122
123
124
125
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using ICSharpCode.Core.Presentation;
namespace ICSharpCode.SharpDevelop.Gui
{
/// <summary>
/// Simple implementation of IOptionPanel with support for OptionBinding markup extensions.
/// </summary>
public class OptionPanel : UserControl, IOptionPanel, IOptionBindingContainer,INotifyPropertyChanged
{
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
static OptionPanel()
{
MarginProperty.OverrideMetadata(typeof(OptionPanel),
new FrameworkPropertyMetadata(new Thickness(2, 0, 4, 0)));
}
public OptionPanel()
{
this.Resources.Add(
typeof(GroupBox),
new Style(typeof(GroupBox)) { Setters = {
new Setter(GroupBox.PaddingProperty, new Thickness(3, 3, 3, 7))
}});
this.Resources.Add(typeof(CheckBox), GlobalStyles.WordWrapCheckBoxStyle);
this.Resources.Add(typeof(RadioButton), GlobalStyles.WordWrapCheckBoxStyle);
}
public virtual object Owner { get; set; }
readonly List<OptionBinding> bindings = new List<OptionBinding>();
void IOptionBindingContainer.AddBinding(OptionBinding binding)
{
this.bindings.Add(binding);
}
public virtual object Control {
get {
return this;
}
}
public virtual void LoadOptions()
{
}
public virtual bool SaveOptions()
{
foreach (OptionBinding b in bindings) {
if (!b.Save())
return false;
}
return true;
}
#region INotifyPropertyChanged implementation
protected void RaisePropertyChanged(string propertyName)
{
RaiseInternal(propertyName);
}
protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpresssion)
{
var propertyName = ExtractPropertyName(propertyExpresssion);
RaiseInternal(propertyName);
}
private void RaiseInternal (string propertyName)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
private static String ExtractPropertyName<T>(Expression<Func<T>> propertyExpresssion)
{
if (propertyExpresssion == null)
{
throw new ArgumentNullException("propertyExpresssion");
}
var memberExpression = propertyExpresssion.Body as MemberExpression;
if (memberExpression == null)
{
throw new ArgumentException("The expression is not a member access expression.", "propertyExpresssion");
}
var property = memberExpression.Member as PropertyInfo;
if (property == null)
{
throw new ArgumentException("The member access expression does not access a property.", "propertyExpresssion");
}
var getMethod = property.GetGetMethod(true);
if (getMethod.IsStatic)
{
throw new ArgumentException("The referenced property is a static property.", "propertyExpresssion");
}
return memberExpression.Member.Name;
}
#endregion
}
}