Для прикрепленных свойств методы Get и Set должны быть связаны с именем свойства, а не с классом, который его определяет.
Если свойство может быть прикреплено к элементам произвольно глубоко в визуальном дереве, у меня есть вспомогательная функция, которая работает для меня.
Вот как я бы сделал страницу / абзац:
public class MyPage : Panel
{
// implementation of custom panel excluded for clarity
}
public class Paragraph
{
public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached(
"Text",
typeof(string),
typeof(CustomContainer),
new FrameworkPropertyMetadata(null)
);
public static void SetText(UIElement element, string value)
{
element.SetValue(TextProperty, value);
}
public static string GetText(UIElement element)
{
return (string)element.GetValue(TextProperty);
}
}
XAML:
<ctls.MyPage>
<ctls.Paragraph x:Name="anInstanceOfParagraph">
<StackPanel>
<TextBlock ctls:Paragraph.Text="ChapterTitle" Text="Chapter One: My Early Years"/>
</StackPanel>
</ctls.Paragraph>
</ctls.MyPage>
Чтобы прикрепить свойство в коде:
private void AttachText(TextBlock textElement, string text)
{
Paragraph.SetText(textElement, text);
}
Затем мы находим произвольно вложенные элементы в Paragraph, к которым прикреплено свойство и которые устанавливают определенное значение с помощью помощника:
var elements = WPFHelper.GetChildrenWithPropertySet(anInstanceOfParagraph,
TextProperty,
"IsIntubationCompleted");
Вот вспомогательная функция, статический метод в классе WPFHelper:
/// <summary>
/// Give a property and a control, find all the child controls that
/// have a property (typically an attached property). Optionally,
/// if value !=null, it will search for an item with the property
/// set to a specific value
/// </summary>
/// <param name="parent"></param>
/// <param name="property"></param>
/// <param name="value"></param>
/// <returns></returns>
public static List<DependencyObject> GetChildrenWithPropertySet(DependencyObject parent,
DependencyProperty property, string value = null)
{
var objectsWithPropertySet = new List<DependencyObject>();
if (value == null)
{
objectsWithPropertySet.AddRange(parent.GetAllChildren()
.Where(o => o.ReadLocalValue(property) != DependencyProperty.UnsetValue));
}
else
{
objectsWithPropertySet.AddRange(parent.GetAllChildren()
.Where(o => o.ReadLocalValue(property) != DependencyProperty.UnsetValue &&
((string)o.ReadLocalValue(property)) == value));
}
return objectsWithPropertySet;
}
/// <summary>
/// returns all children in the visual true of a dependency object
/// </summary>
/// <param name="parent"></param>
/// <returns></returns>
public static IEnumerable<DependencyObject> GetAllChildren(this DependencyObject parent)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
// retrieve child at specified index
var directChild = (Visual)VisualTreeHelper.GetChild(parent, i);
// return found child
yield return directChild;
// return all children of the found child
foreach (var nestedChild in directChild.GetAllChildren())
yield return nestedChild;
}
}