我试图弄清楚如何在Applications资源中查找所有不是Freeze="true"的Brush对象。

到目前为止,我所做的是:

var temp = this.FindLogicalChildren<Brush>();
var resourceDictionary = Application.Current.Resources;

var resources = GetResources(resourceDictionary);

var brushes = resources.Cast<DictionaryEntry>()
     .Where(e => e.Value is Brush).Where(e => !(e.Value as Brush)?.IsFrozen ?? false);
foreach (var entry in brushes)
   ....


但是,这种方法不能满足标准。知道我在这里想念什么吗?

最佳答案

所以我发现this cached website似乎可以提供答案:

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj)
    where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                yield return (T)child;
                }
                foreach (T childOfChild in FindVisualChildren<T>(child))
                {
                yield return childOfChild;
                }
            }
        }
    }
    foreach (Freezable item in FindVisualChildren<Freezable>(myWin))
    if( item != null && item.CanFreeze)item.Freeze();

关于c# - 在WPF项目中查找未卡住的画笔,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47785377/

10-13 01:59