以编程方式将按钮的背景设置为图像

Gee*_*eek 5 button imagesource windows-phone-7

如何以编程方式将按钮的背景设置为图像?我知道如何在XAML中做到这一点,但在代码中,我一直卡住,我试过

Button.Background = new ImageBrush{ ImageSource = "source" };
Run Code Online (Sandbox Code Playgroud)

但后来我得到的错误是字符串无法转换为ImageSource.

Div*_*vya 13

Try:
Button.Background = new ImageBrush{ ImageSource = new BitmapImage(new Uri(imgPath, UriKind.Relative)) };
Run Code Online (Sandbox Code Playgroud)