即使图像成功地采用了MVVM WPF路径,它也不会显示



这个问题可能已经在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"类来保留该属性。在那里,我定义了图像属性如下

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

在视图Model类中,构造函数

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:UsersManeeshaDesktopDips Y-knotsimages";
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。我保留了一个单独的模型来保留属性。请帮帮我!!!

您忘记添加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));
}
}
}

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

最新更新