c# - 在由多个 View 模型绑定(bind)的 View 中绑定(bind)图像不起作用

标签 c# wpf xaml mvvm

嗨,我有下面的窗口,它绑定(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/

相关文章:

c# - 如何动态创建功能区选项卡?

c# - 如何使用 LINQ 查询不包含来自另一个列表的子字符串条目的字符串列表

c# - .Net Core 页面未缓存并单击浏览器后退按钮时出现 “ERR_CACHE_MISS” 错误页面

c# - 我可以用欧洲符号存储我的 DateTime 吗?

c# - 让我的 wpf 应用程序全屏(覆盖窗口的任务栏和标题栏)

c# - SplashScreen.Close(Timespan.FromMilliseconds(int)) : Is there an Event dispatched at Timespan Complete?

c# - 使用 XamlReader.Parse() 从字符串读取 xaml 时使用转换器

c# - 如何实现调用自身的方法?

wpf - 转到 XAML 中的定义

c# - 如何使用 Rg.Plugins.Popup 将 XAML 和模板合并到一个 C# 模板中?