简体   繁体   中英

Using attached properties in WPF

I am trying to create a Read Only attach property in WPF that will calculate the total Visual Child count of the control. The benefit of this is not important to me, it's being able to use attach properties properly!

Firstly I've declared my property like so:

internal static readonly DependencyPropertyKey TotalChildCountPropertyKey =
        DependencyProperty.RegisterAttachedReadOnly("TotalChildCount", typeof(int), typeof(MyAttachClass), new FrameworkPropertyMetadata(0));

    public static readonly DependencyProperty TotalChildCountProperty = TotalChildCountPropertyKey.DependencyProperty;


    public static int GetTotalChildCount(DependencyObject obj)
    {
        return (int)obj.GetValue(TotalChildCountProperty);
    }

    public static void SetTotalChildCount(DependencyObject obj, int value)
    {
        obj.SetValue(TotalChildCountPropertyKey, value);
    }

I also have a recursive method declared else where like so:

public static class Recursive
{
    public static IEnumerable<DependencyObject> GetAllChildren(DependencyObject obj)
    {
        List<DependencyObject> col = new List<DependencyObject>();
        GetAllChildrenImp(obj, col);
        return col;

    }

    private static void GetAllChildrenImp(DependencyObject current,     List<DependencyObject> col)
    {
        if (current != null)
        {
            col.Add(current);

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(current); i++ )
            {
                GetAllChildrenImp(VisualTreeHelper.GetChild(current, i), col);
            }
        }   
    }
}

Now I wish to use this method to assign a value to the GetTotalChildCount property, but I cannot figure out the best way of doing. I could add an event handler to the dependency property changes, but this will never fire because I will only be reading from the value in xaml.

Here's how I am using it in xaml:

<TextBox DataContext="{RelativeSource Self}" Text="{Binding local:MyAttachClass.TotalChildCount}"></TextBox>

So to summarize. I wish to set a DependencyObjects TotalChildCount attach property and then be able to bind to it in xaml. As it stands this is not working, the GetTotalChildCount is not even getting hit.

Oh this is my first question, hopefully I was clear enough

You can try it this way

Attached property

public class MyAttachClass
{
    public static readonly DependencyProperty TotalChildCountProperty = DependencyProperty.RegisterAttached("TotalChildCount", typeof(int), typeof(MyAttachClass), 
    new PropertyMetadata(-1, OnTotalChildCountChanged));

    public static int GetTotalChildCount(DependencyObject obj)
    {
        return (int)obj.GetValue(TotalChildCountProperty);
    }

    public static void SetTotalChildCount(DependencyObject obj, int value)
    {
        obj.SetValue(TotalChildCountProperty, value);
    }

    public static void OnTotalChildCountChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        TextBox txt = sender as TextBox;
        if (txt != null)
        {
            var children = Recursive.GetAllChildren(txt);
            txt.Text = children.Count().ToString();
        }
    }
}

And the xaml as

<TextBox local:MyAttachClass.TotalChildCount="0" ></TextBox>

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM