【发布时间】:2016-06-14 06:50:36
【问题描述】:
您好,我在下面有一个窗口,它绑定到包含多个视图模型的视图模型,如下所示
<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>
然后,我将此窗口中的 Image 绑定设置为 TestImage,该绑定位于 TestModel 中,可在 TestViewModel 中找到。这是 Xaml 中的绑定代码和模型中的 Image 获取/设置代码
Xaml:
<Image Source="{Binding Path=TestModel.TestImage}" />
EDIT EntireTestModel 应请求添加
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));
}
}
}
图像总是在代码中设置并返回正常,但在运行时根本没有出现在视图中,这表明我的绑定有问题,但我看不出它在哪里,因为它是一个非常简单的绑定。
任何人都知道这是为什么,或者他们能告诉我绑定哪里出错了吗?
【问题讨论】:
-
您在 Visual Studio 的“输出”窗格中看到任何绑定错误吗?
-
你真的应该粘贴你的
TestModel代码。我认为这可能是问题的根源。此外,是什么导致所涉及的各种属性(TestModel和TestImage)发生变化?是否为整个链中的所有内容设置了更改通知? -
据我所见,输出窗格中没有绑定错误,我现在将粘贴模型代码。这些属性都是通过执行命令来更改的,该命令将
TestImage的属性设置为从Bitmap转换而来的BitmapImage(我知道代码的工作方式与之前在几个BitmapImages上使用过的一样,并且全部工作)