为什么这个绑定不能在MVVM中工作?
本文关键字:MVVM 工作 不能 绑定 为什么 | 更新日期: 2023-09-27 17:53:10
我正在从WPF视图设置到ViewModel对象的绑定,但是绑定似乎没有被触发。我在MVVM中没有做太多的工作,所以我想我应该问一下,当页面加载时,绑定到图像的源属性是否有原因。
页面的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));
}
}
}
视图模型在app.xaml中实例化,并被打印页面绑定。
查看调试输出窗口。看到任何绑定错误了吗?他们基本上告诉你你需要什么。
如果没有显示调试输出窗口,可以使用CTRL + Alt + O来启用它。
为进一步的信息添加xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
在您的XAML和diag:PresentationTraceSources.TraceLevel=High
绑定:
<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"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
[...]
DataContext="{Binding Main, Source={StaticResource PrintPage}}">
:
<Image Source="{Binding ImageDisplay, diag:PresentationTraceSources.TraceLevel=High}" />
如果绑定使用UpdateSourceTrigger=PropertyChanged
,您还应该确保绑定的属性实现INotifyPropertyChanged
。否则它们将不会更新。
你甚至可以创建一个dummyConverter:
public class DebugDummyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Debugger.Break();
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Debugger.Break();
return value;
}
}
并将其添加到绑定Converter={StaticResource DebugDummyConverter}
中以获取有关绑定值的信息,但这仅在绑定正确设置时有效,否则您将无法获得任何信息。