我正在尝试将图像添加到 WPF - C# 中的切换按钮。问题是我正在处理的任务根本无法使用 XAML 完成。我试图将 Content 属性设置为图像,但我得到的只是一个普通的切换按钮,这对我的事业毫无帮助。
myToggleButton = new ToggleButton();
myImage = new Image();
BitmapImage bmi = new BitmapImage();
bmi.BeginInit();
bmi.UriSource = new Uri("myImageResource.bmp", UriKind.Relative);
bmi.EndInit();
myImage.Source = bmi;
myToggleButton.Content = myImage;
希望我提供了足够的信息,如果没有,请询问更多。
更新@Phil Wright:
当我为这样的图片做广告时:
myImage = new Image();
BitmapImage bmi = new BitmapImage();
bmi.BeginInit();
bmi.UriSource = new Uri("myImageResource.bmp", UriKind.Relative);
bmi.EndInit();
myImage.Source = bmi;
有用...
更新@Matt West:
myGrid.Children.add(MyToggleButton); // This gives me an empty ToggleButton
myGrid.Children.add(MyImage); // This gives me an image with content