Change WPF button background image programmatically

前端 未结 2 1412
野趣味
野趣味 2021-01-14 11:11

I\'m trying to create a

2条回答
  •  無奈伤痛
    2021-01-14 11:42

    A few ways spring to mind:

    1. Access the style at runtime and traverse it to find the image
    2. Give the image a name and reference that at runtime and just change the source

    The best solution I believe is to create a usercontrol and have the image source as a dependency property see: http://www.codeproject.com/Articles/224230/Exploring-the-use-of-Dependency-Properties-in-User

    That way you'll have a ready-to-use "custom" control that supports what you're after and allows you to just reference it directly and use bindings, that way you avoid the messy solutions from using a style.


    After discussion and expansion on requirements, please see:

    How can I access ResourceDictionary in wpf from C# code?

提交回复
热议问题