forked from snoopwpf/snoopwpf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PertinentPropertyFilter.cs
79 lines (66 loc) · 2.7 KB
/
PertinentPropertyFilter.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
// (c) Copyright Cory Plotts.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http:https://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using System;
using System.ComponentModel;
using System.Windows;
namespace Snoop
{
public class PertinentPropertyFilter
{
public PertinentPropertyFilter(object target)
{
this.target = target;
this.element = this.target as FrameworkElement;
}
public bool Filter(PropertyDescriptor property)
{
if (this.element == null)
return true;
// Filter the 20 stylistic set properties that I've never seen used.
if (property.Name.Contains("Typography.StylisticSet"))
return false;
AttachedPropertyBrowsableForChildrenAttribute attachedPropertyForChildren = (AttachedPropertyBrowsableForChildrenAttribute)property.Attributes[typeof(AttachedPropertyBrowsableForChildrenAttribute)];
AttachedPropertyBrowsableForTypeAttribute attachedPropertyForType = (AttachedPropertyBrowsableForTypeAttribute)property.Attributes[typeof(AttachedPropertyBrowsableForTypeAttribute)];
AttachedPropertyBrowsableWhenAttributePresentAttribute attachedPropertyForAttribute = (AttachedPropertyBrowsableWhenAttributePresentAttribute)property.Attributes[typeof(AttachedPropertyBrowsableWhenAttributePresentAttribute)];
if (attachedPropertyForChildren != null)
{
DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(property);
if (dpd == null)
return false;
FrameworkElement element = this.element;
do
{
element = element.Parent as FrameworkElement;
if (element != null && dpd.DependencyProperty.OwnerType.IsInstanceOfType(element))
return true;
}
while (attachedPropertyForChildren.IncludeDescendants && element != null);
return false;
}
else if (attachedPropertyForType != null)
{
// when using [AttachedPropertyBrowsableForType(typeof(IMyInterface))] and IMyInterface is not a DependencyObject, Snoop crashes.
// see http:https://snoopwpf.codeplex.com/workitem/6712
if (attachedPropertyForType.TargetType.IsSubclassOf(typeof(DependencyObject)))
{
DependencyObjectType doType = DependencyObjectType.FromSystemType(attachedPropertyForType.TargetType);
if (doType != null && doType.IsInstanceOfType(this.element))
return true;
}
return false;
}
else if (attachedPropertyForAttribute != null)
{
Attribute dependentAttribute = TypeDescriptor.GetAttributes(this.target)[attachedPropertyForAttribute.AttributeType];
if (dependentAttribute != null)
return !dependentAttribute.IsDefaultAttribute();
return false;
}
return true;
}
private object target;
private FrameworkElement element;
}
}