嗨,我有下面的窗口,它绑定(bind)到包含多个 View 模型的 View 模型,如下所示
<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"
然后我将 Window 的 DataContext 设置为
TestViewModel
如下所示 <Window.DataContext>
<viewModel:TestViewModel />
</Window.DataContext>
然后我将此窗口中的图像绑定(bind)设置为
TestImage
。可在 TestModel
中找到在 TestViewModel
中找到这是 Xaml 中的绑定(bind)代码和模型中的图像获取/设置代码xml:
<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));
}
}
}
图像总是在代码中设置并返回正常,但在运行时根本没有出现在 View 中,这表明我的绑定(bind)有问题,但我看不出它在哪里,因为它是一个非常简单的绑定(bind)。
任何人都知道这是为什么,或者他们能告诉我装订哪里出了问题吗?
最佳答案
当您创建/加载 BitmapImage 集时 bitmapImage.CacheOption = BitmapCacheOption.OnLoad
.
关于c# - 在由多个 View 模型绑定(bind)的 View 中绑定(bind)图像不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35729927/