gpt4 book ai didi

c# - 即使成功采用了路径,图像也不会显示-MVVM WPF

转载 作者:行者123 更新时间:2023-12-03 11:01:33 25 4
gpt4 key购买 nike

这个问题可能已经在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;
}
}
}

在 View 模型类中,构造函数
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。我正在保留一个单独的模型来保留属性。
请帮我!!!

最佳答案

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

}

关于c# - 即使成功采用了路径,图像也不会显示-MVVM WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61006537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com