سؤال

how can I programmatically set the background of a button to be an image? I know how to do it in XAML, but in code, I keep getting stuck, I tried

Button.Background = new ImageBrush{ ImageSource = "source" };

but then I get the error that string cannot be converted to ImageSource.

هل كانت مفيدة؟

المحلول

Try:
Button.Background = new ImageBrush{ ImageSource = new BitmapImage(new Uri(imgPath, UriKind.Relative)) };
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top