Этот вопрос, возможно, уже задавался в 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));
}
}
}
Я не знаю, что происходит не так. Я также пытаюсь добавить UpdateSourceTrigger в коде xaml. это не сработало. Я редактировал код, я использую INotifyPropertyChanged в классе ViewModel. Я держу отдельную модель, чтобы сохранить атрибуты. Пожалуйста, помогите мне !!!