C# 如何绑定图像源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11745696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I bind an Image Source?
提问by B-Rad
Here is what I have so far:
这是我到目前为止所拥有的:
<Image Source="{Binding ImageSource"} />
<Button Content"Text" ImageSource="path/image.png" />
I know something isn't right here. I guess I can't see where ImageSource is defined.
我知道这里有些不对劲。我想我看不到 ImageSource 的定义位置。
I have several of these buttons and just want to have a unique image for each one. I have a button template that I am using and it works great for the text.
我有几个这样的按钮,只想为每个按钮都有一个独特的图像。我有一个我正在使用的按钮模板,它非常适合文本。
<Label Content="TemplateBinding Content" />
Thanks for all your help!
感谢你的帮助!
采纳答案by Mare Infinitus
In your case, it can be very easy!
在您的情况下,这可能非常容易!
Add the images as resources to your project, then in XAML use something like the following:
将图像作为资源添加到您的项目中,然后在 XAML 中使用如下所示的内容:
<Button HorizontalAlignment="Left" Margin="20,0,0,20" VerticalAlignment="Bottom" Width="50" Height="25">
<Image Source="image.png" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
</Image>
</Button>
Or, the more complicated way:
或者,更复杂的方法:
If you use the MVVM Pattern, you can do the following
如果使用 MVVM 模式,则可以执行以下操作
In your XAML:
在您的 XAML 中:
<Button Focusable="False" Command="{Binding CmdClick}" Margin="0">
<Image Source="{Binding ButtonImage}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
</Image>
</Button>
In your ViewModel:
在您的视图模型中:
private Image buttonImage;
public Image ButtonImage
{
get
{
return buttonImage;
}
}
And somewhere in the constructor of your ViewModel or the initialisation of it:
在 ViewModel 的构造函数或它的初始化中的某处:
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri("image.png", UriKind.Relative);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
buttonImage = new Image();
buttonImage.Source = src;
回答by J.B.D
In your XAML:
在您的 XAML 中:
<Button Focusable="False" Command="{Binding CmdClick}" Margin="0">
<Image Source="{Binding ImageSource,UpdateSourceTrigger=PropertyChanged} HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
</Image>
</Button>
In your ViewModel:
在您的视图模型中:
private BitmapImage _ImageSource;
public BitmapImage ImageSource
{
get { return this._ImageSource; }
set { this._ImageSource = value; this.OnPropertyChanged("ImageSource"); }
}
private void OnPropertyChanged(string v)
{
// throw new NotImplementedException();
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(v));
}
public event PropertyChangedEventHandler PropertyChanged;
And somewhere in the constructor of your ViewModel or the initialisation of it:
在 ViewModel 的构造函数或它的初始化中的某处:
string str = System.Environment.CurrentDirectory;
string imagePath = str + "\Images\something.png";
this.ImageSource = new BitmapImage(new Uri(imagePath, UriKind.Absolute));
OR:
或者:
string imagePath = "\Images\something.png";
this.ImageSource = new BitmapImage(new Uri(imagePath, UriKind.Relative));

