【问题标题】:The Image is not showing even if it successfully takes the path - MVVM WPF即使成功采用路径,图像也没有显示 - MVVM WPF
【发布时间】:2020-04-03 06:54:32
【问题描述】:

这个问题可能已经在 Stackoverflow 中提出,但我浏览了所有这些问题,但我的图像没有显示在窗口中,即使正确地选择了路径。我正在使用浏览器按钮导入图像。另外,我最近开始学习 MVVM 架构和 WPF 这是我的 Xaml 代码;

<!--Image block-->
                <Label Grid.Row="5" Grid.Column="1" Style="{StaticResource LabelStyles}">Image :</Label>
                <DockPanel Grid.Row="5" Grid.Column="2" VerticalAlignment="Center" MinHeight="30" LastChildFill="True">
                    <Button DockPanel.Dock="Left" Style="{StaticResource ButtonStyle}" Content="Browse" Margin="0,0,5,0" Command="{Binding Path=ImageCommand, UpdateSourceTrigger=PropertyChanged}"/>
                <TextBox Name="FileBrowser" DockPanel.Dock="Right" Style="{StaticResource StyleControl}"  HorizontalAlignment="Stretch" Padding="5" Text="{Binding Path= Patient.ImageView}"/>
                </DockPanel>

                <!--Image view block-->
                <Image Name="ImageViewer" Grid.Row="6" Grid.Column="2" HorizontalAlignment="Left" MinWidth="95" MaxWidth="150" MinHeight="95" MaxHeight="150" Margin="0,0,0,10" Source ="{Binding Path= Patient.ImageView}"/>

我保留了一个单独的模型“PatientRecordDetailsModel”类来保留该属性。在那里我定义了 Image 属性如下

public class PatientRecordDetailsModel
    {
private ImageSource m_imgSource;
public ImageSource ImageView { 
            get
            {
                return m_imgSource;
            }
            set
            {
                m_imgSource = value;
            }
        }
}

在视图模型类中,构造函数

class PatientRecordDetailsViewModel : INotifyPropertyChanged
{
public PatientRecordDetailsViewModel()
        {

            Patient = new PatientRecordDetailsModel();
        }

// this is the Model property which takes the user inputs

        public PatientRecordDetailsModel Patient
        {
            get { return m_patient; }
            set
            {
                m_patient = value;
                 OnPropertyChange("Patient");

            }
        }

// The browser button command 
public ICommand ImageCommand
        {
            get => new PatientRecordDetailsCommand(param => getImage(), param => canImage());
        }

// the two methods
 private bool canImage()
        {
            return true;
        }

 private void getImage()
        {

            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = @"C:\Users\Maneesha\Desktop\Dips Y-knots\images\";
            dlg.Filter = "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" +
                            "All files (*.*)|*.*";
            if (dlg.ShowDialog() == true)
            {

                string m_fileName = dlg.FileName;
                BitmapImage bitmap = new BitmapImage(new Uri(m_fileName));
                Patient.ImageView = bitmap;

            }
        }

        // INotify     
 public event PropertyChangedEventHandler PropertyChanged;
 private void OnPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
}

我不知道出了什么问题。我也尝试在 xaml 代码中添加 UpdateSourceTrigger。它没有用。我编辑了代码,我在 ViewModel 类中使用了 INotifyPropertyChanged。我保留一个单独的模型来保留属性。 请帮帮我!!!

【问题讨论】:

  • 您需要触发一个NotifyPropertyChanged 事件,在该事件中为Patient 属性设置图像。
  • 嗨,我确实在视图模型中触发了 NotifyPropertyChanged,即使我没有包含它,现在我对其进行了编辑。它仍然没有工作。谢谢!

标签: c# wpf xaml mvvm data-binding


【解决方案1】:

您忘记添加 OnPropertyChange("ImageView");

public class PatientRecordDetailsModel : INotifyPropertyChanged
    {
private ImageSource m_imgSource;
public ImageSource ImageView { 
            get
            {
                return m_imgSource;
            }
            set
            {
                m_imgSource = value;
                OnPropertyChange("ImageView");
            }
        }


        // INotify     
 public event PropertyChangedEventHandler PropertyChanged;
 private void OnPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

}

【讨论】:

  • 嗨,谢谢 Ugur!,但我将此属性保留在单独的类中,并使用视图模型创建类的实例并调用 OnPropertyChange("Patient") 进行更改用户输入一次。
  • 如果你想将更改传递给 viewmodel,你的模型也应该实现 INotifyPropertyChanged 接口
  • 您好,我找到了解决方案,正如您所说,imageView 中也应该有 OnPropertyChange 方法。因此,我没有在模型类中执行此操作,而是在 ViewModel 中创建了另一个 Image 属性并从中调用。同时我也将值设置为模型类图像属性
  • 在ViewModel类``` public ImageSource ImageSourceView { get { return m_imageShow; } 设置 { m_imageShow = 值;患者.ImageView = 值; OnPropertyChange("ImageSource"); } } 将位图保存到 ImageSourceView。
  • 是的,你也可以这样做
【解决方案2】:

从磁盘加载图像后,您必须通知 WPF Patient(图像)的内容已更改。加载图片后,我会调用 OnPropertyChange(nameof(Patient)) 方法。

【讨论】:

  • 我立即调用 Patient 对象,以获取属性更改。我必须在下面调用 OnPropertyChange("ImageView") 吗?
猜你喜欢
  • 1970-01-01
  • 2023-03-12
  • 2022-01-04
  • 2021-12-11
  • 2022-01-26
  • 2016-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多