如何在我的解决方案中找到所有隐式WPF样式?

问题描述:

我想使用Visual Studio的Find in Files(或其他一些机制)来查找我的解决方案中的所有隐式WPF样式(所有没有Key的样式并因此全局应用)。这如何实现?如何在我的解决方案中找到所有隐式WPF样式?

+0

他们确实有一个Key,默认的一个。实际上,当你编写类似''没有指定KEY的东西时,它会以完全相同的方式工作。我可能是错的,但我认为Control('TextBox')的名称是一个Key。 U可以查看文档以找到确切的答案。 – 52hertz

+0

你检查了答案吗? – AnjumSKhan

我们必须检查Style资源的Key。如果Key的值为System.Type,并且其基类为System.Windows.FrameworkElement,则表示它是隐含的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); 
    } 
}