如何以编程方式更改边框背景图像 [英] How to change Border Background image programmatically
问题描述
我正在WPF c#中创建一个媒体播放器应用程序. 我正在使用Media Element来做到这一点.
I am creating a media player app in WPF c#. I am using Media Element to do this.
无论如何,我已经使用<Border> </Border>
在某些地方添加边框.
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>
我想以编程方式将ImageSource更改为其他图片,该怎么做?
I want to change the ImageSource to some other picture programmatically, how to do that?
我尝试过但没有成功.
所以对于每首歌曲,<ImageBrush ImageSource="Images\music.png"
中的图像都会改变.
So for every song the image in <ImageBrush ImageSource="Images\music.png"
is changed.
预先感谢
Shafi
推荐答案
为ImageBrush分配名称:
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));
或者只需将Border的Background
属性的值强制转换为ImageBrush类型:
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));
这篇关于如何以编程方式更改边框背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!