【问题标题】:How can you get an image to update as a databinded value changes?如何在数据绑定值更改时更新图像?
【发布时间】: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();
                }
            }
        }
    }

【问题讨论】:

标签: c# wpf image binding


【解决方案1】:

您没有按应有的方式实现它,请尝试以下操作:

private string _Title_Image;
public string Title_Image {
    get
    {
        return _Title_Image;
    }
    set
    {
        if (value != this.Title_Image)
        {
            _Title_Image = value;
            NotifyPropertyChanged();
        }
    }
}

【讨论】:

  • 谢谢 - 我意识到了,所以我只是在更新我的帖子,正如你在这里回复的那样。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-15
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
相关资源
最近更新 更多