[英]Why isn't this binding working in MVVM?
我正在从 WPF 视图设置到 ViewModel object 的绑定,但绑定似乎没有触发。 我在 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 中实例化,并由打印页面绑定。
查看您的调试 Output window。 您是否看到任何绑定错误? 他们几乎告诉你你需要什么。
如果 Debug Output Window 未显示,您可以使用CTRL + Alt + O启用它。
有关详细信息,请在 XAML 和diag:PresentationTraceSources.TraceLevel=High
中添加xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
到您的绑定:
<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}
中以获取有关绑定值的信息,但这仅在正确设置绑定时才有效,否则您将无法获得任何信息。
有关详细信息,请参阅http://www.wpf-tutorial.com/data-binding/debugging/ 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.