Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF:是否可以获取某个附加属性集上的所有元素?_Wpf_Properties_Dependencies - Fatal编程技术网

WPF:是否可以获取某个附加属性集上的所有元素?

WPF:是否可以获取某个附加属性集上的所有元素?,wpf,properties,dependencies,Wpf,Properties,Dependencies,我创建了一个自定义的附加属性,它应该设置在某些不相关的元素上。此附加属性的目的是保持所有这些元素的宽度相同。因此,当这些元素中的任何一个更改宽度时,它应该使用此附加属性集更新所有其他元素。 我能够通过UIPropertyMetadata设置的“DP property changed event handler”跟踪对附加属性的更改,因此当任何元素更改宽度时,我将收到该通知。然后我需要做的是更新具有此附加属性集的所有其他元素 所以我想知道有没有可能做到这一点?是否有方法枚举具有特定附加属性集的所有

我创建了一个自定义的附加属性,它应该设置在某些不相关的元素上。此附加属性的目的是保持所有这些元素的宽度相同。因此,当这些元素中的任何一个更改宽度时,它应该使用此附加属性集更新所有其他元素。 我能够通过UIPropertyMetadata设置的“DP property changed event handler”跟踪对附加属性的更改,因此当任何元素更改宽度时,我将收到该通知。然后我需要做的是更新具有此附加属性集的所有其他元素

所以我想知道有没有可能做到这一点?是否有方法枚举具有特定附加属性集的所有DependencyObject实例


我想这是一个有点高级的WPF内容,但在我的WPF应用程序中它是非常具体的要求。

您可以枚举可视化树,并检查是否在每个元素上设置了属性。大概是这样的:

var objectsWithPropertySet = new List<DependencyObject>();
if (RootVisual.ReadLocalValue(FocusIdProperty) != DependencyProperty.UnsetValue)
    objectsWithPropertySet.Add(RootVisual);

objectsWithPropertySet.AddRange(RootVisual.GetAllChildren()
    .Where(o => o.ReadLocalValue(FocusIdProperty) != DependencyProperty.UnsetValue));
public static class VisualTreeHelperExtensions
{
    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;
        }
    }
}
var objectsWithPropertySet=new List();
if(RootVisual.ReadLocalValue(FocusIdProperty)!=DependencyProperty.UnsetValue)
objectsWithPropertySet.Add(RootVisual);
objectsWithPropertySet.AddRange(RootVisual.GetAllChildren())
其中(o=>o.ReadLocalValue(FocusIdProperty)!=DependencyProperty.UnsetValue));
GetAllChildren()扩展方法的实现方式如下:

var objectsWithPropertySet = new List<DependencyObject>();
if (RootVisual.ReadLocalValue(FocusIdProperty) != DependencyProperty.UnsetValue)
    objectsWithPropertySet.Add(RootVisual);

objectsWithPropertySet.AddRange(RootVisual.GetAllChildren()
    .Where(o => o.ReadLocalValue(FocusIdProperty) != DependencyProperty.UnsetValue));
public static class VisualTreeHelperExtensions
{
    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;
        }
    }
}
公共静态类VisualTreeHelperExtensions
{
公共静态IEnumerable GetAllChildren(此DependencyObject父对象)
{
for(int i=0;i