wpf image resources and changing image in wpf control at runtime

后端 未结 2 1224
天涯浪人
天涯浪人 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:32

    This is an addition to the accepted answer: When working within a ViewModel from MVVM, make sure to use the FindResource from the view where the resource directory is added.

    
        
            
                
                    
                
            
        
        
            
        
        
            
                
                    
    

    My view in this case is a window (I know not correct MVVM ;-) )

    Image img = new Image();                                    
    img.Source = (ImageSource)WindowReference.FindResource("Pluse"); 
    

    Here the WindowReference is a reference to My.MainWindow.

提交回复
热议问题