简体   繁体   English

如何绑定图像源?

[英]How do I bind an Image Source?

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! 感谢你的帮助!

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: 在您的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;

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: 在您的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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM