How to change the background image of button on runtime?

前端 未结 3 1419
逝去的感伤
逝去的感伤 2021-01-19 21:12

I m stuck with a problem. I want to change the background image of button on runtime. I got the solution for changing the color but i want to change the image.

The c

3条回答
  •  一向
    一向 (楼主)
    2021-01-19 22:08

    Utilize VisualStates for this kind of UI state switching. Your code behind would look like this:

    public void buttonCase(object sender, RoutedEventArgs e)
    {
        if (((App)App.Current).appControler.m_Mode == Controller.textMode.Letters)
        {
            ((App)App.Current).appControler.buttonCase(sender, e);
            switch (((App)App.Current).appControler.m_case)
            {
            case Controller.caseMode.Upper:
                VisualStateManager.GoToState( this, "UpperCase", true );
                break;
            case Controller.caseMode.Lower:
                VisualStateManager.GoToState( this, "LowerCase", true );
                break;
            }
        }
    }  
    

    And your xaml would look like this:

    
    
        
            
                
                    
                        
                            
                                
                                    
                                        
                                            Visible
                                        
                                    
                                
                                
                                    
                                        
                                            Collapsed
                                        
                                    
                                
                            
                        
                        
                    
                
                
                
            
        
    
    
    
        
            
                
                
                    
                        
                            
                                
                                    40
                                
                            
                        
                    
                
            
        
    
        
        

    I wrote the code as an answer to a similar question here:toggle button with different images

    I know that defining VisualStates can look quite cumbersome, but in the end you can keep your code behind quite clean from switching visual appearance of all the ui bits and pieces.

提交回复
热议问题