根据用户在窗口内的哪个导航页面,我有两个窗口的背景图像。图像位于我的项目文件夹内的位置:设置背景URI图像不工作
<NavigationWindow.Background>
<ImageBrush ImageSource="/Blah.MyApp;component/Resources/Images/MyImage1.jpg" />
</NavigationWindow.Background>
这正确显示图像:
/Resources/Images/MyImage1.jpg
如果我用IDE来选择一个固定的背景图片,然后将XAML文件与更新。
然而,正如我希望的图像切换,我创建了一个视图模型为如下暴露一个URI属性窗口(其中实现INotifyPropertyChanged):
private readonly string _image1 = "pack://application:,,,/Resources/Images/MyImage1.jpg";
private readonly string _image2 = "pack://application:,,,/Resources/Images/MyImage2.jpg";
public MainNavWindowViewModel()
{
SetImage1();
}
private Uri _backgroundImg;
public Uri BackgroundImg
{
get
{
return _backgroundImg;
}
private set
{
_backgroundImg = value;
OnPropertyChanged("BackgroundImg");
}
}
public void SetImage1()
{
BackgroundImg = new Uri(_image1);
}
public void SetImage2()
{
BackgroundImg = new Uri(_image2);
}
在主窗口中的XAML文件,我已将NavigationWindow.Background替换为:
<NavigationWindow.Background>
<ImageBrush ImageSource="{Binding Path=BackgroundImg, Mode=OneWay}" />
</NavigationWindow.Background>
并将Windows'DataContext设置为ViewModel。
但是,这个功能只是在我的窗口上显示一个黑色的背景(当我注释掉数据绑定ViewModel到窗口的那一行时,这是相同的行为)。
我将ViewModel设置为窗口构造函数的第一行中的Datacontext。如果我在它上面放置一个断点,它会执行这些行并且看起来没问题。
任何想法正在发生什么,或者我怎么能找出它出错的地方?
TIA
感谢您的回复。现在不再是问题,但我使用.Net 4。 – millie 2012-05-17 15:40:44