How do I match my controls' styles to the current theme? (WPF)

前端 未结 2 563
北海茫月
北海茫月 2020-12-16 04:00

If I create a custom control with WPF, how can I provide styles for the control that match the currently applied theme (Aero, Luna, Classic, etc.)?

For example I\'d

2条回答
  •  暖寄归人
    2020-12-16 04:18

    You can use different themes in WPF by loading/unloading resource dictionaries. These dictionaries should contain the styles for your controls. When you swap out dictionaries, WPF will apply the styles to your controls.

    for example if this were in WhiteStyle.xaml and you loaded it, your textblocks would all display text in a white-colored font.

    
    

    if you swapped it out for BlackStyle.xaml which contains

    
    

    your textblocks would display text in a black-colored font. WPF handles alot of the details for us, we just have to tell it what the details are.

    Swapping out resource dictionaries is realatively simple, and I leave that to you to figure out. Googling "WPF Themes" is a good place to start.

提交回复
热议问题