For attached properties, the Get and Set methods should be associated with the property name and not the class that defines it.
If a property can be attached to elements arbitrarily deep in a visual tree, I have a helper function that works for me.
Here's how I would do page/paragraph:
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);
}
}
The 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>
To attach a property in code:
private void AttachText(TextBlock textElement, string text)
{
Paragraph.SetText(textElement, text);
}
Then we find arbitrarily nested elements within Paragraph that have the property attached and set to a specific value using a helper:
var elements = WPFHelper.GetChildrenWithPropertySet(anInstanceOfParagraph,
TextProperty,
"IsIntubationCompleted");
Here is the helper function, a static method in the WPFHelper class:
/// <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;
}
}