如何在XAML对象上设置附加属性MyPage.MyParagraph.Text

时间:2019-04-12 15:03:26

标签: xaml attached-properties

是否可以在对象深几层的位置获取/设置附加属性?

虚拟示例:

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

还有我的虚拟课

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));
     }
}

我尝试了诸如Paragraph2之类的不同格式,将XAML调用包装在括号中,并且建议使用非同寻常的'+'语法here,但我不断收到类似以下错误消息:“属性'MyPage.Paragraph1.Text'在XML名称空间'....'中不存在,“在类型'MyPage'中找不到可连接属性'Paragraph1',”和“必须不是嵌套类。”

1 个答案:

答案 0 :(得分:1)

对于附加属性,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;
    }
}