Click here to Skip to main content
14,984,194 members
Home / Discussions / WPF
   

WPF

 
PinnedHOW TO ANSWER A QUESTION PinPopular
Chris Maunder16-Jul-09 3:09
cofounderChris Maunder16-Jul-09 3:09 
PinnedHow to get an answer to your question PinPopular
Chris Maunder16-Jul-09 3:05
cofounderChris Maunder16-Jul-09 3:05 
QuestionWPF DataGrid - Stretch Row Details Vertically Pin
Kevin Marois29-Jul-21 7:39
professionalKevin Marois29-Jul-21 7:39 
QuestionGet MainWindow from Executing Assembly Pin
Kevin Marois23-Jul-21 8:59
professionalKevin Marois23-Jul-21 8:59 
AnswerRe: Get MainWindow from Executing Assembly Pin
Richard Deeming27-Jul-21 23:18
mveRichard Deeming27-Jul-21 23:18 
GeneralRe: Get MainWindow from Executing Assembly Pin
Kevin Marois28-Jul-21 10:04
professionalKevin Marois28-Jul-21 10:04 
GeneralCan anyone recommend a free WPF editor control? Pin
Mike Hankey23-Jul-21 4:25
professionalMike Hankey23-Jul-21 4:25 
GeneralRe: Can anyone recommend a free WPF editor? Pin
Slacker00723-Jul-21 4:39
professionalSlacker00723-Jul-21 4:39 
GeneralRe: Can anyone recommend a free WPF editor? Pin
Richard Andrew x6423-Jul-21 4:45
professionalRichard Andrew x6423-Jul-21 4:45 
GeneralRe: Can anyone recommend a free WPF editor? Pin
Mike Hankey23-Jul-21 4:50
professionalMike Hankey23-Jul-21 4:50 
GeneralRe: Can anyone recommend a free WPF editor? Pin
Slacker00723-Jul-21 5:01
professionalSlacker00723-Jul-21 5:01 
GeneralRe: Can anyone recommend a free WPF editor control? Pin
Richard Deeming27-Jul-21 23:16
mveRichard Deeming27-Jul-21 23:16 
GeneralRe: Can anyone recommend a free WPF editor control? Pin
Mike Hankey28-Jul-21 2:05
professionalMike Hankey28-Jul-21 2:05 
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 

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.