Attach Image/ImageBrush from code behind

后端 未结 1 1903
执笔经年
执笔经年 2021-01-06 02:11

I\'m trying to add an Image as the background of a UserControl. Depending on the value of a variable I need to change that background but whatever the path or Uri format I u

1条回答
  •  旧时难觅i
    2021-01-06 02:37

    I can reproduce your issue when changing the background of a user control.

    The current workaround I used was changing the background of root UIElement in the control.

    
        
            
        
        
            Hello World
            
            
        
    
    

    public sealed partial class MyUserControl : UserControl
    {
        public MyUserControl()
        {
            this.InitializeComponent();
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ImageBrush imgB = new ImageBrush();
    
            BitmapImage btpImg = new BitmapImage();
    
            btpImg.UriSource = new Uri(@"ms-appx:///images/bg-light-blue.png");
    
            imgB.ImageSource = btpImg;
    
            container.Background = imgB;
        }
    }
    

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