How to set Control Template in code?

前端 未结 3 1962
滥情空心
滥情空心 2020-12-01 12:05

I have this in XAML


    

I want to achieve

3条回答
  •  旧时难觅i
    2020-12-01 12:47

    If you need to change the button image only then you can do one thing.

    1. Create a dependency property which will represent when you want to change the image (a bool) or may be you can create an enum which has all possible images say
    2. Enum Images { Image1 = 0, Image2 = 1, Image2 = 3}. Create a dependency property "CurrentButtonImage" of this type which will be associated with button.

    Now in XAML use this in button template

    On property Change of CurrentButtonImage update the image of button (in code behind) using

    CurrentImagePropertyChangedhandler(....,...)  
    {  
        switch(CurrentButtonImage)  
        {  
            case "Image1" :  
              this._ButtonImage.Fill = (DrawingBrush)csd.FindResource("Image1DrawingBrush");
              break;
        }
    }
    

提交回复
热议问题