c# - 为什么这个绑定(bind)在 MVVM 中不起作用?

标签 c# xaml mvvm

我正在从 WPF View 设置到 ViewModel 对象的绑定(bind),但绑定(bind)似乎没有触发。我在 MVVM 中没有做太多工作,所以我想我想看看是否有原因导致在页面加载时绑定(bind)到图像的源属性没有触发。

这是页面的 XAML:

<Page x:Class="DallasPrintManager.PrintPage"
  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:DallasPrintManager.ViewModel"
  mc:Ignorable="d" 
  d:DesignHeight="300" d:DesignWidth="900"
  DataContext="{Binding Main, Source={StaticResource PrintPage}}">
<Grid >
    <Grid.ColumnDefinitions>
        <ColumnDefinition  />
        <ColumnDefinition Width="200" />
    </Grid.ColumnDefinitions>
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
        <Image Source="{Binding ImageDisplay}" />
    </ScrollViewer>
</Grid>
</Page>

这是 ViewModel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using System.Drawing;
using System.Windows.Input;
using DallasPrintManager.Commands;
using System.Net;
using System.IO;
using System.Windows;

public class PrintPageViewModel : INotifyPropertyChanged
{
    private BitmapImage _imageDisplay;

    public PrintPageViewModel()
    {
        ImageDisplay = getImage();
    }

    private BitmapImage getImage()
    {
        try
        {
            WebClient wc = new WebClient();
            byte[] imageData = wc.DownloadData("http://localhost/TestImage.tif");
            MemoryStream ms = new MemoryStream();
            ms.Write(imageData, 0, imageData.Length);
            System.Windows.Media.Imaging.BitmapImage wpfImg = new System.Windows.Media.Imaging.BitmapImage();
            wpfImg.BeginInit();
            wpfImg.StreamSource = ms;
            wpfImg.EndInit();
            return wpfImg;
            //return (Bitmap)Bitmap.FromStream(ms);
        }
        catch (WebException e)
        {
            MessageBox.Show("Error fetching document:\n\n" + e.Message);
            return null;
        }
        catch (Exception e)
        {
            if (e.Source == "System.Drawing")
                MessageBox.Show("Error reading document.");
            Console.Out.Write(e);
            return null;
        }
    }

    public BitmapImage ImageDisplay
    {
        get
        {
            return _imageDisplay;
        }
        set
        {
            if (value != _imageDisplay)
            {
                _imageDisplay = value;
                OnPropertyChanged("ImageDisplay");
            }
        }

    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
        }
    }

}

View 模型在 app.xaml 中实例化,并由打印页面绑定(bind)。

最佳答案

查看调试输出窗口。您是否看到任何绑定(bind)错误?他们几乎告诉你你需要什么。

关于c# - 为什么这个绑定(bind)在 MVVM 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5808460/

相关文章:

c# - 将 SQLite 数据填充到 UWP 应用程序中

c# - 搜索后覆盖记录 -MVVM WPF

WPF ComboBox 绑定(bind)到非字符串对象

c# - 获取已安装 Msi 的产品代码

c# - Linq 加入并排序

c# - 如何在 WinRT XAML C# 中克隆 UIElement?

c# - 无法加载文件或程序集 'System.Windows.Interactivity'

c# - 从反射方法 C# 中获取值

c# - ASP MVC 将 ApplicationUser 添加到通用存储库

wpf - 客户服务架构