Using a resource image in code behind

前端 未结 1 1971
感动是毒
感动是毒 2021-01-05 13:04

I need to dynamically change the background image applied to one of my buttons, but can\'t figure out how. The images are added to the project and have their Build Action se

相关标签:
1条回答
  • 2021-01-05 13:46

    You have to build the image as a Resource NOT an embedded resource. Resource is there specifically to be used by WPF projects.

    To use it in procedural code:

     buttonUnits.Background = new ImageBrush(new BitmapImage(new Uri("pack://application:,,,/Images/InchDOWN.png")));
    

    This is a lot easier to do in XAML which I recommend.

    Edit

    I forgot a forward slash before Images, that could be the problem. Here is a MSDN article about pack Uris if you need more info.

    Maybe you should post a question stating what exactly you're trying to accomplish and hopefully discover different approaches to your multi-state button problem.

    0 讨论(0)
提交回复
热议问题