“path”不是Image.source的有效属性

时间:2010-07-22 14:32:21

标签: c# wpf pixelsense

我正试图通过这种方式更改xaml设置的背景图像:

      <s:SurfaceWindow.Resources>
    <ImageBrush x:Key="WindowBackground" 
Stretch="None" Opacity="0.6" ImageSource="pack://application:,,,/Resources/img/bg/Default.jpg"/>
  </s:SurfaceWindow.Resources>

在方法中使用以下代码:

        sessionWindow.SetValue(ImageBrush.ImageSourceProperty, "..//..//Resources//img//bg//Aqua.jpg");

其中sessionWindow是实际窗口。 它会在标题

中抛出异常

1 个答案:

答案 0 :(得分:1)

ImageBrush.ImageSource property的类型为ImageSource

因此,您需要将其设置为ImageSource个实例 而且,你的路径是错误的。

例如:

sessionWindow.SetValue(ImageBrush.ImageSourceProperty,
     new BitmapImage(
        new Uri(@"..\..\Resources\img\bg\Aqua.jpg", UriKind.Relative)
     )
);

但是,这实际上不会改变背景 - Window没有ImageSource属性。
相反,您应该设置Window的Background属性,如下所示:

sessionWindow.Background = new ImageBrush {
     ImageSource = new BitmapImage(
        new Uri(@"..\..\Resources\img\bg\Aqua.jpg", UriKind.Relative)
     )
};