【发布时间】:2020-09-10 19:03:16
【问题描述】:
我有一个类cTailoredReading 和一个图像的字符串属性。现在,下面的代码有效。当我创建类的实例并设置datacontext 时,图像控件会更新并显示指定的图像。
但是,当我调用DisplayedImageChange 时,它并没有更新图像控件。
任何建议/指示都会非常有帮助,因为我已经为此苦苦挣扎了几天。
cTailoredReading.cs:
class cTailoredReading
{
public cTailoredReading(string sTitle, string sFocus)
{
Title = sTitle;
Focus = sFocus;
Title_Image = @"C:\Users\local-paul\Pictures\Elly\3rd Birthday Photoshoot\BABY0363.JPG";
}
public void DisplayedImageChange()
{
Title_Image = @"C:\Users\local-paul\Pictures\Elly\3rd Birthday Photoshoot\BABY0364.JPG";
}
public string Title_Image { get; set; }
}
XAML:
<Image x:Name="ResourceMainImage" Source="{Binding Title_Image}" Width="80" Height="80"/>
已解决
感谢 CFun,我通过以下更改解决了这个问题:
cTailoredReading.cs:
class cTailoredReading : INotifyPropertyChanged
{
public cTailoredReading(string sTitle, string sFocus)
{
Title = sTitle;
Focus = sFocus;
Title_Image = @"C:\Users\local-paul\Pictures\Elly\3rd Birthday Photoshoot\BABY0363.JPG";
}
public void DisplayedImageChange()
{
Title_Image = @"C:\Users\local-paul\Pictures\Elly\3rd Birthday Photoshoot\BABY0364.JPG";
}
//public string Title_Image { get; set; }
private string _Title_Image;
public string Title_Image {
get
{
return _Title_Image;
}
set
{
if (value != this.Title_Image)
{
_Title_Image = value;
NotifyPropertyChanged();
}
}
}
}
【问题讨论】:
-
你需要实现inotifypropertychanged 很多例子都是可用的,只要搜索
inotifypropertychanged。