Reputation: 11827
I'd like to use Visual Studio's Find in Files (or some other mechanism) to find all the implicit WPF Styles in my solution (all the styles that do not have a Key and thus apply themselves globally). How can this be accomplished?
Upvotes: 0
Views: 239
Reputation: 9827
We have to check the Key
of the Style
resource. If the value of Key
is of type System.Type
and its base class is System.Windows.FrameworkElement
, it means it is an implicit Style
.
static List<Style> _styles = new List<Style>();
private void Button_Click(object sender, RoutedEventArgs e)
{
// Check for Application
var appResDict = Application.Current.Resources;
foreach (DictionaryEntry entry in appResDict)
{
if ((entry.Key is System.Type) && ((Type)entry.Key).IsSubclassOf(typeof(System.Windows.FrameworkElement)))
_styles.Add((Style)entry.Value);
}
// Check for Window
var resDict = this.Resources;
foreach (DictionaryEntry entry in resDict)
{
if ((entry.Key is System.Type) && ((Type)entry.Key).IsSubclassOf(typeof(System.Windows.FrameworkElement)))
_styles.Add((Style)entry.Value);
}
// Check for all other controls
MainWindow.EnumVisual(this);
MessageBox.Show(_styles.Count.ToString());
}
// Enumerate all the descendants of the visual object.
static public void EnumVisual(Visual myVisual)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
{
// Retrieve child visual at specified index value.
Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);
// Check for implicit style
if (childVisual is FrameworkElement)
{
FrameworkElement elem = (FrameworkElement)childVisual;
var resDict = elem.Resources;
foreach (DictionaryEntry entry in resDict)
{
if ((entry.Key is System.Type) && ((Type)entry.Key).IsSubclassOf(typeof(System.Windows.FrameworkElement)))
_styles.Add((Style)entry.Value);
}
}
// Enumerate children of the child visual object.
EnumVisual(childVisual);
}
}
Upvotes: 1