---
# System prepended metadata

title: WPF 利用可能な ResourceDictionary　の Key を列挙する。　 2021-05-25
tags: [ResourceDictionary, WPF, 列挙]

---

---
lang: ja-jp
breaks: true
---
# WPF 利用可能な ResourceDictionary　の Key を列挙する。　 2021-05-25

```csharp=
        /// <summary>
        /// 
        /// </summary>
        /// <param name="resourceDictionary"></param>
        /// <param name="createString"></param>
        /// <returns></returns>
        public static IEnumerable<string> EnumerateResourceDictionaryEntry(
            ResourceDictionary resourceDictionary,
            Func<ResourceDictionary, DictionaryEntry, StringBuilder> createString = null
            )
        {
            if (resourceDictionary != null)
            {
                foreach (DictionaryEntry de in resourceDictionary)
                {
                    StringBuilder str = new StringBuilder();
                    if (createString != null)
                    {
                        str = createString(resourceDictionary, de);
                    }
                    else
                    {
                        str
                            .Append(resourceDictionary.Source.OriginalString)
                            .Append(' ')
                            .Append(de.Key)
                            .Append(" : ")
                            .Append(de.Value.ToString())
                            .Append(' ')
                            .Append(de.Value)
                            .AppendLine();
                    }
                    yield return str.ToString();
                }

                foreach (ResourceDictionary rd in resourceDictionary.MergedDictionaries.Reverse())
                {
                    if (rd != null)
                    {
                        IEnumerable<string> iEnm = EnumerateResourceDictionary(rd, createString);
                        foreach (var item in iEnm)
                        {
                            yield return item;
                        }
                    }
                }
            }
        }
```

###### tags: `WPF` `ResourceDictionary` `列挙`
