[英]Binding an image within a view bound by multiple viewmodels not working
嗨,我下面有一个窗口绑定到一个包含多个视图模型的视图模型,如下所示
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewModel="clr-namespace:TestProject.ViewModels"
然后,将窗口的DataContext设置为TestViewModel
,如下所示
<Window.DataContext>
<viewModel:TestViewModel />
</Window.DataContext>
然后,我在此窗口TestImage
Image的绑定设置为TestImage
,该绑定在TestViewModel
中的TestModel
找到。这是Xaml中的绑定代码以及Model中的Image get / set
XAML:
<Image Source="{Binding Path=TestModel.TestImage}" />
编辑整个TestModel
而不是通过请求添加
public class Coupon : INotifyPropertyChanged
{
private static DateTime _selectedDay = DateTime.Now;
public DateTime SelectedDay
{
get { return _selectedDay; }
set
{
if (value.Equals(_selectedDay)) return;
_selectedDay = value;
OnPropertyChanged("SelectedDay");
}
}
private BitmapImage _testImage;
public BitmapImage TestImage
{
get { return _testImage; }
set
{
if (Equals(value, _testImage)) return;
_testImage= value;
OnPropertyChanged("TestImage");
}
}
private static bool _isNeeded = true;
public bool IsNeeded
{
get { return _guaranteeEarlyPrices; }
set { _guaranteeEarlyPrices = value; }
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
该图像始终在代码中设置并返回正确,但在运行时根本不会出现在视图中,这表明我的绑定有问题,但是我看不到它的绑定非常简单。
有人知道这是为什么吗,或者他们可以告诉我绑定在哪里出错了?
创建/加载BitmapImage时,请设置bitmapImage.CacheOption = BitmapCacheOption.OnLoad
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.