1

I have several buttons in my WPF Window. How can I target all buttons inside the window regardless if they are inside Stack panels or Dock Panels. I want to target all of them to be able to change the background color of all of them through settings.

Rather than targeting each individual name of the button, is there another method?

MCSharp
  • 1,068
  • 1
  • 14
  • 37

2 Answers2

2

Have a look at this question Find all controls in WPF Window by type

It shows how to get all the controls in a WPF application, and then you can set your background color of your buttons.

foreach (Button btn in FindVisualChildren<Button>(window))
{
    btn.Background = new SolidColorBrush(Colors.Black);
}

FindVisualChildren is from the linked answer.

Community
  • 1
  • 1
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
  • I have voted to delete my answer as this one is clearly correct and you were first to answer , mine would only cause confusion – TimothyP Dec 27 '12 at 03:03
2

Define a style for button in resources without giving key name. It will be automatically applied to all buttons in window.

 <Style TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Red"/>
    </Style>

hope it helps..

D J
  • 6,908
  • 13
  • 43
  • 75