在受多个视图模型绑定的视图中绑定图像不起作用

本文关键字:视图 绑定 图像 不起作用 模型 | 更新日期: 2023-09-27 18:36:52

嗨,我下面的窗口绑定到包含多个视图模型的视图模型,如下所示

<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"

然后,我将窗口的数据上下文设置为如下所示的TestViewModel

 <Window.DataContext>
    <viewModel:TestViewModel />
</Window.DataContext>

然后,我将此窗口中图像的绑定设置为TestImage该绑定可在TestViewModel中找到TestModel中找到 这是 Xaml 中的绑定代码和模型中的图像获取/设置的代码

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.CacheOption = BitmapCacheOption.OnLoad .