wpf image resources and changing image in wpf control at runtime

后端 未结 2 1217
天涯浪人
天涯浪人 2021-02-08 06:46

I would like to know exactly how to dynamically use a Dictionary Resource in the C# code behind - ie.. I would like to load images at runtime from an image resource within a dic

2条回答
  •  孤独总比滥情好
    2021-02-08 07:39

    First, make sure you've defined your image resources like this:

    
        images/image1.jpg
        images/image2.jpg
    
    

    Secondly, I'm assuming that your WPF dictionary is in its own file. Now you have to make sure you've merged your dictionary into your main window's XAML (skip this step if your resource dictionary is defined inside of the window's XAML). In your window's XAML file, make sure you have something like this:

    
        
            
                
            
        
    
    

    Now, in your code-behind, you can use the FindResource() method to locate your image resource by it's key name (the value of the x:Key attribute on the ImageSource in the resource dictionary) like so:

    imageControl.Source = (ImageSource)FindResource("image1");
    

    Hope this helps!

提交回复
热议问题