【问题标题】:How to change Border Background image programmatically如何以编程方式更改边框背景图像
【发布时间】:2018-01-15 20:49:54
【问题描述】:

我正在 WPF c# 中创建一个媒体播放器应用程序。 我正在使用 Media Element 来执行此操作。

反正我用<Border> </Border>在一些地方加了边框。

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

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

我想以编程方式将 ImageSource 更改为其他图片,该怎么做?

我试过了,但没有成功。

所以对于每一首歌,&lt;ImageBrush ImageSource="Images\music.png" 中的图像都会改变。

提前致谢

沙菲

【问题讨论】:

    标签: wpf border imagebrush


    【解决方案1】:

    为 ImageBrush 指定名称:

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

    然后在代码中使用命名成员:

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

    或者简单地将 Border 的 Background 属性的值转换为 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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-28
        • 2014-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多