c# - 在 WPF 中使用附加属性

标签 c# wpf xaml binding

我正在尝试在 WPF 中创建一个只读附加属性,该属性将计算控件的总可视子项数。这样做的好处对我来说并不重要,它能够正确使用附加属性!

首先我已经这样声明了我的属性(property):

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

我还有一个递归方法声明在其他地方,像这样:

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

现在我想使用此方法为 GetTotalChildCount 属性赋值,但我想不出最好的方法。我可以向依赖项属性更改添加一个事件处理程序,但这永远不会触发,因为我只会读取 xaml 中的值。

这是我在 xaml 中使用它的方式:

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

总结一下。我希望设置一个 DependencyObjects TotalChildCount 附加属性,然后能够在 xaml 中绑定(bind)到它。就目前而言,这是行不通的,GetTotalChildCount 甚至都没有受到影响。

哦,这是我的第一个问题,希望我足够清楚

最佳答案

你可以这样试试

附加属性

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

而 xaml 为

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

希望对您有所帮助!

关于c# - 在 WPF 中使用附加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33963378/

相关文章:

c# - 在 WPF 中将按钮绑定(bind)到 bool 值

c# - 如何在运行时从项目中包含的 XAML 文件中获取文本?

c# - UWP - 应用程序文件夹中的图像 Uri

c# - 有没有办法在代码隐藏中解析 UWP 中的矢量路径几何?

c# - 如何在 C# 中传递和使用泛型对象?

c# - 为什么不调用 Task<T>.Result 死锁?

c# - 如何使用Parallel.For?

wpf - 滚动查看器的子元素阻止用鼠标滚轮滚动?

c# - 一维柏林噪声?

c# - 数据网格:用户无法编辑或选择行