How to change Border Background image programmatically

爷,独闯天下 提交于 2019-12-12 01:07:21

问题


I am creating a media player app in WPF c#. I am using Media Element to do this.

Anyways, I have used <Border> </Border> to add border some places.

    <Border Name="hej1">
                <Border.Background>
                    <ImageBrush ImageSource="Images\music.png"  Stretch="None"/>
                </Border.Background>

                <MediaElement ..../> 
    </Border>

I want to change the ImageSource to some other picture programmatically, how to do that?

I have tried but no success.

So for every song the image in <ImageBrush ImageSource="Images\music.png" is changed.

Thanks in advance

Shafi


回答1:


Assign a Name to the ImageBrush:

<ImageBrush x:Name="imageBrush" ImageSource="Images\music.png" Stretch="None"/>

Then use the named member in code:

var filename = @"Images\title.png";
imageBrush.ImageSource = new BitmapImage(new Uri(filename, UriKind.Relative));

Or simply cast the value of the Border's Background property to type ImageBrush:

var imageBrush = (ImageBrush)hej1.Background;
var filename = @"Images\title.png";
imageBrush.ImageSource = new BitmapImage(new Uri(filename, UriKind.Relative));



回答2:


BitmapImage img = new BitmapImage(new Uri(@"Images\myimage.png"));
ImageBrush image = new ImageBrush();
image.ImageSource = img;
Border.Background =image;


来源:https://stackoverflow.com/questions/17136655/how-to-change-border-background-image-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!