1

Topic: How to implement a command for Window in wpf?

I welcome. A simple example. The form wpf, implemented in MVVM (). At form loading it is necessary to execute a command. Saw, how in other situations it is possible to use library System. Windows. Interactivity. The Same mechanism I try to apply here, but the command with event does not communicate, method LoadWindow is not caused. Help to understand, please. Whether it is possible so to set a command here? What do I do not so? Probably, it is possible to set it somehow in another way? I will be very grateful for council. <Window x:Class = "ClassLibrary1.ViewWindow4.ViewWindowClass4" xmlns = "http://schemas.microsoft.com/winfx/2006 … esentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc = "http://schemas.openxmlformats.org/marku … ility/2006" xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable = "d" MinHeight = "100" MinWidth = "100" Height = "Auto" Width = "100" SizeToContent = "WidthAndHeight" WindowStartupLocation = "CenterScreen" xmlns:i = "http://schemas.microsoft.com/expression … eractivity"> <i:Interaction. Triggers> <i:EventTrigger EventName = "Loaded"> <i:InvokeCommandAction Command = "{Binding Path=WWindowLoadedCommand, Mode=TwoWay}"/> </i:EventTrigger> </i:Interaction. Triggers> <Grid> </Grid> </Window> namespace ClassLibrary1.ViewModelLib4 {public class ViewModelClass4: Notifier, IViewModel4 {ModelClass4 model; private ICommand wWindowLoadedCommand; public ICommand WWindowLoadedCommand {get {return wWindowLoadedCommand;} set {wWindowLoadedCommand = value; NotifyPropertyChanged ("WWindowLoadedCommand");}} public ViewModelClass4 (ModelClass4 _model) {model = _model; WWindowLoadedCommand = new WindowLoadedCommandClass (this);} public void LoadWindow () {}}///<summary> a class-notifikator for a binding to fields of forms </summary> public class Notifier: INotifyPropertyChanged {#region structures and classes #endregion #region renominal///<summary> event of change of field value in shape </summary> public event PropertyChangedEventHandler PropertyChanged = delegate {}; #endregion #region functions///<summary> a handler function of event of change of field value in shape </summary> <param name = ""> </param> protected void NotifyPropertyChanged (string propertyName) {PropertyChanged (this, new PropertyChangedEventArgs (propertyName));} #endregion}} namespace ClassLibrary1.CommandLib4 {///<summary> a command of loading of a window </summary> public class WindowLoadedCommandClass: IWindowLoadedCommand4 {#region structures and classes #endregion #region variables///<summary> object model-representation private </summary> private IViewModel4 viewModel;///<summary> object model-representation public </summary> public IViewModel4 ViewModel {get {return viewModel;}}///<summary> the event happening at origin of changes, influencing, whether the given command </summary> public event EventHandler CanExecuteChanged = delegate {} should be fulfilled; #endregion #region functions///<summary> the designer of a class SelectionActionChangedCommandClass</summary>///<param name = "_ view_model"> object of model </param> public WindowLoadedCommandClass (IViewModel4 _view_model) {viewModel = _view_model;}///<summary> </summary> public bool CanExecute (object parameter) {return true;} ///<summary> </summary> public void Execute (object parameter) {viewModel. LoadWindow ();} #endregion}}

2

Re: How to implement a command for Window in wpf?

Hello, lesovick, you wrote: L> I Welcome. L> a simple example. The form wpf, implemented in MVVM (). At form loading it is necessary to execute a command. Saw, how in other situations it is possible to use library System. Windows. Interactivity. The Same mechanism I try to apply here, but the command with event does not communicate, method LoadWindow is not caused. Help to understand, please. Whether it is possible so to set a command here? What do I do not so? Probably, it is possible to set it somehow in another way? I will be very grateful for council. Tried to implement event KeyDown also precisely. The command works. Then that event Loaded cannot so be implemented, since the command binding at a stage of the loaded window is not carried out yet?. I.e. event Loaded in a command generally in any way cannot be implemented? Or all the same it is possible?

3

Re: How to implement a command for Window in wpf?

Hello, lesovick, you wrote: L> Hello, lesovick, you wrote: L>> I Welcome. L>> a simple example. The form wpf, implemented in MVVM (. At form loading it is necessary to execute a command. Saw, how in other situations it is possible to use library System. Windows. Interactivity. The Same mechanism I try to apply here, but the command with event does not communicate, method LoadWindow is not caused. Help to understand, please. Whether it is possible so to set a command here? What do I do not so? Probably, it is possible to set it somehow in another way? I will be very grateful for council. L> tried to implement event KeyDown also precisely. The command works. Then that event Loaded cannot so be implemented, since the command binding at a stage of the loaded window is not carried out yet?. I.e. event Loaded in a command generally in any way cannot be implemented? Or all the same it is possible? And what for actually, to make something once at form discovery it is possible and in designer VM.

4

Re: How to implement a command for Window in wpf?

Hello, Qulac, you wrote: Q> And what for actually to make something once at form discovery it is possible and in designer VM. , I it invented to solve other task. The task such. We load the form. Its size is defined automatically depending on the content. However, I want, that the turned out size of the form became minimum (I want to appropriate MinHeight and MinWidth). This size I cannot learn before form loading (in the designer of the form I too do not know it). How in that case to me correctly to appropriate MinHeight and MinWidth? The code such: <Window x:Class = "ClassLibrary1.ViewWindow4.ViewWindowClass4" xmlns = "http://schemas.microsoft.com/winfx/2006 … esentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc = "http://schemas.openxmlformats.org/marku … ility/2006" xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable = "d" MinHeight = "{Binding Path=MinWindowHeight, Mode=TwoWay}" MinWidth = "{Binding Path=MinWindowWidth, Mode=TwoWay}" Height = "{Binding Path=WindowHeight, Mode=TwoWay}" Width = "{Binding Path=WindowWidth, Mode=TwoWay}" SizeToContent = "WidthAndHeight" WindowStartupLocation = "CenterScreen" xmlns:i = "http://schemas.microsoft.com/expression … eractivity"> <i:Interaction. Triggers> <i:EventTrigger EventName = "Loaded"> <i:InvokeCommandAction Command = "{Binding Path=WWindowLoadedCommand, Mode=TwoWay}"/> </i:EventTrigger> </i:Interaction. Triggers> <Grid> </Grid> </Window> namespace ClassLibrary1.ViewModelLib4 {public class ViewModelClass4: Notifier, IViewModel4 {ModelClass4 model; private ICommand wWindowLoadedCommand; public ICommand WWindowLoadedCommand {get {return wWindowLoadedCommand;} set {wWindowLoadedCommand = value; NotifyPropertyChanged ("WWindowLoadedCommand");}}///<summary> the minimum size of a window private </summary> private Int32 minWindowHeight;///<summary> the minimum size of a window public </summary> public Int32 MinWindowHeight {get {return minWindowHeight;} set {minWindowHeight = value; NotifyPropertyChanged ("MinWindowHeight");}}///<summary> the minimum size of a window private </summary> private Int32 minWindowWidth;///<summary> the minimum size of a window public </summary> public Int32 MinWindowWidth {get {return minWindowWidth;} set {minWindowWidth = value; NotifyPropertyChanged ("MinWindowWidth");}}///<summary> the size of a window private </summary> private Int32 windowHeight;///<summary> the size of a window public </summary> public Int32 WindowHeight {get {return windowHeight;} set {windowHeight = value; NotifyPropertyChanged ("WindowHeight");} }///<summary> the size of a window private </summary> private Int32 windowWidth;///<summary> the size of a window public </summary> public Int32 WindowWidth {get {return windowWidth;} set {windowWidth = value; NotifyPropertyChanged ("WindowWidth");}} public ViewModelClass4 (ModelClass4 _model) {model = _model; WWindowLoadedCommand = new WindowLoadedCommandClass (this);} public void LoadWindow () {}}}

5

Re: How to implement a command for Window in wpf?

Invented such candidate solution of the given task: namespace ClassLibrary1.ViewModelLib4 {public class ViewModelClass4: Notifier, IViewModel4 {ModelClass4 model;///<summary> a flag for individual initial formation of the minimum height of a window </summary> private Boolean minHeightFlag;///<summary> a flag for individual initial formation of the minimum width of a window </summary> private Boolean minWidthFlag; private ICommand wWindowLoadedCommand; public ICommand WWindowLoadedCommand {get {return wWindowLoadedCommand;} set {wWindowLoadedCommand = value; NotifyPropertyChanged ("WWindowLoadedCommand");}}///<summary> the minimum size of a window private </summary> private Int32 minWindowHeight;///<summary> the minimum size of a window public </summary> public Int32 MinWindowHeight {get {return minWindowHeight;} set {minWindowHeight = value; NotifyPropertyChanged ("MinWindowHeight");} }///<summary> the minimum size of a window private </summary> private Int32 minWindowWidth;///<summary> the minimum size of a window public </summary> public Int32 MinWindowWidth {get {return minWindowWidth;} set {minWindowWidth = value; NotifyPropertyChanged ("MinWindowWidth");}}///<summary> the size of a window public </summary> public Int32 WindowHeight {get {return windowHeight;} set {if (value! = double. NaN && value! = 0 && minHeightFlag) {MinWindowHeight = value; minHeightFlag = false;} windowHeight = value; NotifyPropertyChanged ("WindowHeight");}}///<summary> the size of a window private </summary> private Int32 windowWidth;///<summary> the size of a window public </summary> public Int32 WindowWidth {get {return windowWidth;} set {if (value! = double. NaN && value! = 0 && minWidthFlag) {MinWindowWidth = value; minWidthFlag = false;} windowWidth = value; NotifyPropertyChanged ("WindowWidth");} } public ViewModelClass4 (ModelClass4 _model) {model = _model; minHeightFlag = true; minWidthFlag = true; WWindowLoadedCommand = new WindowLoadedCommandClass (this);} public void LoadWindow () {}}}

6

Re: How to implement a command for Window in wpf?

Hello, lesovick, you wrote: L> Hello, Qulac, you wrote: Q>> And what for actually to make something once at form discovery it is possible and in designer VM. L> , I it invented to solve other task. The task such. L> we load the form. Its size is defined automatically depending on the content. However, I want, that the turned out size of the form became minimum (I want to appropriate MinHeight and MinWidth). This size I cannot learn before form loading (in the designer of the form I too do not know it). How in that case to me correctly to appropriate MinHeight and MinWidth? The code such: . it is necessary to learn the size of the form installed automatically what to specify it as minimum possible? It probably is better for making in a class of the form not  here vm. ViewModel for another.

7

Re: How to implement a command for Window in wpf?

Hello, Qulac, you wrote: Q> . it is necessary to learn the size of the form installed automatically what to specify it as minimum possible? It probably is better for making in a class of the form not  here vm. ViewModel for another. Taki-priplyol. It would be desirable to save purity of a pattern... Thanks big for the help in a problem.

8

Re: How to implement a command for Window in wpf?

Hello, lesovick, you wrote: L> [...] Speak  in 4.7.1 https://github.com/Microsoft/dotnet/iss … -340804604 I tested with Win10 1709.16299.19, VS2017 v15.5.0 Preview 2.0.net v4.7.02556 and there is no such issue. It seems to be fixed with the.net 4.7.1 update... <<RSDN@Home 1.3.108 alpha 5 rev. 56>>