Click here to Skip to main content
15,039,809 members
Home / Discussions / WPF
   

WPF

 
QuestionInstalling Test Mode vs Production Mode Pin
Kevin Marois19-Jul-21 8:55
professionalKevin Marois19-Jul-21 8:55 
QuestionRouted Event Args Null Pin
Kevin Marois28-Jun-21 16:28
professionalKevin Marois28-Jun-21 16:28 
AnswerRe: Routed Event Args Null Pin
Richard Deeming28-Jun-21 22:34
mveRichard Deeming28-Jun-21 22:34 
QuestionHow to change background color in combobox in code behind? Pin
Cường Nguyễn Văn 202123-Jun-21 6:56
MemberCường Nguyễn Văn 202123-Jun-21 6:56 
QuestionMake textbox fill up and resizie Pin
Acuena21-Jun-21 20:59
MemberAcuena21-Jun-21 20:59 
AnswerRe: Make textbox fill up and resizie Pin
Richard Deeming21-Jun-21 21:58
mveRichard Deeming21-Jun-21 21:58 
AnswerRe: Make textbox fill up and resizie Pin
Gerry Schmitz22-Jun-21 7:07
mveGerry Schmitz22-Jun-21 7:07 
Question[Problem] Why can not Pass Image between 2 Views by using the same ViewModel (MVVM) Pin
Oscar Tsai18-Jun-21 7:34
MemberOscar Tsai18-Jun-21 7:34 
The purpose of this code is open image file in a page and display it in another page.
I had tried set ViewModel by using Singleton Pattern, but it didn't work....
If I integrate to one View, it can display the image correctly. But I need to separate the views.

I have no idea what's wrong in this MVVM code....and how to fix it?
Please point me how can I do or let me know what keyword I can search the toturial/sample.
Thank you all.
--
The OpenFile button is on DisplayControlView and display on DisplayView.
The View Schema is as below:
MainWindow.xaml (window)
|
|__ReviewView.xaml (page)
  |
  |__DisplayControlView.xaml (page)
  |
  |__DisplayPanelView.xaml (page)
    |
    |__DisplayView.xaml (page)


--
DisplayControlView.xaml
XML
<Page.DataContext>
    <vm:DisplayViewModel/>
</Page.DataContext>
...
<Button x:Name="btnDpOpen" Style="{DynamicResource ButtonStyle}" Width="120" Command="{Binding OpenFile}">
...

--
DisplayView.xaml
XML
<Page.DataContext>
    <vm:DisplayViewModel/>
</Page.DataContext>
...
<Image x:Name="imgDisplayView" Source="{Binding ImagePath}">
	<Image.RenderTransform>
		<TransformGroup>
			<TranslateTransform/>
			<RotateTransform/>
			<ScaleTransform/>
		</TransformGroup>
	</Image.RenderTransform>
</Image>
...

--
DisplayViewModel.cs
C#
class DisplayViewModel : ViewModelBase
{
	private DisplayImageModel image { get; set; }

	private ObservableCollection<DisplayImageModel> imagelist = new ObservableCollection<DisplayImageModel>();
	public ObservableCollection<DisplayImageModel> ImageList
	{
		get { return imagelist; }
		set
		{
			imagelist = value;
			OnPropertyChanged();
		}
	}

	public string ImagePath
	{
		get { return image.Path; }
		set
		{
			if (image.Path != value)
			{
				image.Path = value;
				OnPropertyChanged();
			}
		}
	}

	public DisplayViewModel()
	{
		image = new DisplayImageModel();
		ImagePath = @"C:\Users\oscartsai\Desktop\lenna.png";
	}

	public bool CanExecute()
	{
		return true;
	}

	public RelayCommand OpenFile
	{
		get { return new RelayCommand(openFile, CanExecute); }
	}

	private void openFile()
	{
		string[] picExtName = new string[] { ".PNG", ".JPG", "JEPG", "BMP" };

		OpenFileDialog dlgOpenFile = new OpenFileDialog() 
		{ Filter = "Picture|*.jpg;*.jpeg;*.bmp;*.png|All File|*.*" };
		if (dlgOpenFile.ShowDialog() != true)
		{
			return;
		}
		if (picExtName.Any(System.IO.Path.GetExtension(dlgOpenFile.FileName).ToUpper().Contains))
		{
			ImagePath = dlgOpenFile.FileName;
			OnPropertyChanged();

		}
	}
}

--
ViewModelBase.cs
C#
public abstract class ViewModelBase : INotifyPropertyChanged
{
	public event PropertyChangedEventHandler PropertyChanged;

	public void SetValue<T>(ref T property, T value, [CallerMemberName] string propertyName = null)
	{
		if (property != null)
		{
			if (property.Equals(value))
			{
				return;
			}
		}

		property = value;
		OnPropertyChanged(propertyName);
	}

	protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
	{
		if (PropertyChanged != null)
		{
			PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}
	}
}

--
RelayCommond.cs
C#
public class RelayCommand : ICommand
{
	readonly Func<bool> _canExecute;
	readonly Action _execute;

	public RelayCommand(Action execute) : this(execute, null)
	{
	}

	public RelayCommand(Action execute, Func<bool> canExecute)
	{
		_execute = execute ?? throw new ArgumentNullException("execute");
		_canExecute = canExecute;
	}
	
	public event EventHandler CanExecuteChanged
	{
		add
		{
			if (_canExecute != null) CommandManager.RequerySuggested += value;
		}
		remove
		{
			if (_canExecute != null) CommandManager.RequerySuggested += value;
		}
	}

	[DebuggerStepThrough]
	public bool CanExecute(object parameter)
	{
		return _canExecute == null ? true : _canExecute();
	}

	public void Execute(object parameter)
	{
		_execute();
	}
}

AnswerRe: [Problem] Why can not Pass Image between 2 Views by using the same ViewModel (MVVM) Pin
Gerry Schmitz19-Jun-21 14:29
mveGerry Schmitz19-Jun-21 14:29 
GeneralRe: [Problem] Why can not Pass Image between 2 Views by using the same ViewModel (MVVM) Pin
Oscar Tsai20-Jun-21 3:20
MemberOscar Tsai20-Jun-21 3:20 
GeneralRe: [Problem] Why can not Pass Image between 2 Views by using the same ViewModel (MVVM) Pin
Gerry Schmitz20-Jun-21 6:01
mveGerry Schmitz20-Jun-21 6:01 
AnswerRe: [Problem] Why can not Pass Image between 2 Views by using the same ViewModel (MVVM) Pin
Richard Deeming20-Jun-21 23:37
mveRichard Deeming20-Jun-21 23:37 
GeneralRe: [Problem] Why can not Pass Image between 2 Views by using the same ViewModel (MVVM) Pin
Oscar Tsai21-Jun-21 4:10
MemberOscar Tsai21-Jun-21 4:10 
GeneralRe: [Problem] Why can not Pass Image between 2 Views by using the same ViewModel (MVVM) Pin
Oscar Tsai21-Jun-21 16:11
MemberOscar Tsai21-Jun-21 16:11 
AnswerRe: [Problem] Why can not Pass Image between 2 Views by using the same ViewModel (MVVM) Pin
michaelbarb23-Sep-21 10:32
Membermichaelbarb23-Sep-21 10:32 
QuestionProblem Setting Focus Pin
Kevin Marois24-May-21 9:39
professionalKevin Marois24-May-21 9:39 
AnswerRe: Problem Setting Focus Pin
Richard Deeming24-May-21 21:56
mveRichard Deeming24-May-21 21:56 
QuestionImpossible to override a value in an animation located in a style? Pin
Mc_Topaz20-May-21 6:51
MemberMc_Topaz20-May-21 6:51 
AnswerRe: Impossible to override a value in an animation located in a style? Pin
Gerry Schmitz20-May-21 8:08
mveGerry Schmitz20-May-21 8:08 
QuestionApp Security Behavior Pin
Kevin Marois20-May-21 6:09
professionalKevin Marois20-May-21 6:09 
QuestionOverride a value in base style's resource Pin
Mc_Topaz20-May-21 2:45
MemberMc_Topaz20-May-21 2:45 
AnswerRe: Override a value in base style's resource Pin
Richard Deeming20-May-21 4:33
mveRichard Deeming20-May-21 4:33 
GeneralRe: Override a value in base style's resource Pin
Mc_Topaz20-May-21 6:00
MemberMc_Topaz20-May-21 6:00 
QuestionApply TextBlock Captions at Runtime Pin
Kevin Marois14-May-21 9:34
professionalKevin Marois14-May-21 9:34 
AnswerRe: Apply TextBlock Captions at Runtime Pin
Peter_in_278014-May-21 13:36
professionalPeter_in_278014-May-21 13:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.