1

Is it possible to get/set attached properties several layers deep in an object?

Dummy example:

<ContentControl local:MyPage.Paragraph1.Text="I'm actually a lot of text"/>

And my dummy classes

public static class MyPage
{
     public static readonly Paragraph1 = new Paragraph();
     public static Paragraph2 {get;} = new Paragraph();
}

public class Paragraph
{
     public readonly DependencyProperty TextProperty;
     public void SetParagraph(DependencyObject obj, string text) => obj.SetValue(TextProperty, text);
     public void GetParagraph(DependencyObject obj) => (string)obj.GetValue(TextProperty);

     public Paragraph()
     {
         //note that this is NOT a static Dependency Property. Instead, each instance of this class will be static.
         TextProperty = DependencyProperty.RegisterAttached("Text", typeof(string), typeof(Paragraph), new PropertyMetadata(string.Empty));
     }
}

I've tried different formats like Paragraph2, wrapping the XAML call in Parentheses, and the unusual '+' syntax suggested here, but I keep getting errors like: "The property 'MyPage.Paragraph1.Text' does not exist in the XML namespace '....'", "The attachable property 'Paragraph1' was not found in type 'MyPage'," and "must not be a nested class."

bwall
  • 984
  • 8
  • 22

1 Answers1

1

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;
    }
}
Kevin Walsh
  • 114
  • 8