2015-11-27 143 views
2

我想在WPF中创建一个只读附加属性,它将计算控件的总可视子项数。这对我来说并不重要,它可以正确使用附加属性!在WPF中使用附加的属性

首先我声明我的财产,像这样:

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中绑定它。就目前而言,这不起作用,GetTotalChildCount甚至没有被击中。

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

+0

您是否需要将孩子添加到TextBox?我认为只有像Grid/StackPanel/DockPanel等容器控件才允许有孩子。 –

+0

我不需要添加孩子。 GetAllChildren方法将返回构成TextBox的所有依赖对象,例如边界等 – Andrew

+0

'GetTotalChildCount()'应至少调用一次。但是如果你不调用'SetTotalChildCount()',它可能不会被再次调用给定的绑定。不幸的是,如果没有一个可靠地再现问题的[mcve],就很难确切知道你想要做什么,而不必介意如何修复代码。 –

回答

-2

,您可以尝试这样

附加属性

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> 

希望能帮助到你!

+0

OP正在实现一个只读属性。以上内容不是只读的(更糟糕的是,通过在附加属性控制本身中对客户端元素的属性更改的行为进行硬编码,完全提供了与客户端元素的过多耦合)。 –