WPF User Control Revisited
-
WPF using MVVM... I have posted similar questions before and honest;y I haven't always uinderstood the responses. Now I have a new requirement from my client and I have to revisit this. Please see this screen shot[^]. On the right is a section of the Job Screen called Job Personnel with 6 combo boxes. The XAML for the Superintendant is shown also. Up till now, the combo box was populated from a list of employees filtered to only those that is IsSupervisor = true. The new requirement is that this drop down, and all others that show employees, will be loaded based on the employee's position. An Employee's position is set in the Employee screen. So, the app will now query all employees for a position code. Positions are stored in a lookup table with a code relevant to each position:
LookupCode SystemCode Caption
empposition accountant Accountant
empposition fldsuper Field Superintendent
empposition foreman Foreman
empposition projectmanager Project Manager
empposition safetyofficer Safety OfficerI now need to add the button next to any combo showing Employees so that the user can pick ANY employee and assign him to the drop down. It's a way to temporarily override the position. Please see this[^] So, on load the combo will be loaded from the lookup table using the code. Then when the user picks a new employee via the dialog, that employee is added to the combo and its Id is assigned to the Job.SuperintendantId. So far I can handle all of this. Here's where I get hung up... I think that this should be done via a control. The control would have the label, a combo, and button. Somewhere I would set the code, probably in the XAML. ALso in the XAML the selected EmployeeId from the control would be exposed and bound to the appropriate field on the job, or whatever entity is using employees. By making this a control I can use it all over the app. What I don't understand is how to create this. I'm guessing the selected Id needs to be a DP so I can bind to it. Can I do in XAML? I'm using MVVM. Do I create a view and VM and work with that? I could use some direction. An example would be great. Many thanks
If it's not broken, fix it until it is
-
WPF using MVVM... I have posted similar questions before and honest;y I haven't always uinderstood the responses. Now I have a new requirement from my client and I have to revisit this. Please see this screen shot[^]. On the right is a section of the Job Screen called Job Personnel with 6 combo boxes. The XAML for the Superintendant is shown also. Up till now, the combo box was populated from a list of employees filtered to only those that is IsSupervisor = true. The new requirement is that this drop down, and all others that show employees, will be loaded based on the employee's position. An Employee's position is set in the Employee screen. So, the app will now query all employees for a position code. Positions are stored in a lookup table with a code relevant to each position:
LookupCode SystemCode Caption
empposition accountant Accountant
empposition fldsuper Field Superintendent
empposition foreman Foreman
empposition projectmanager Project Manager
empposition safetyofficer Safety OfficerI now need to add the button next to any combo showing Employees so that the user can pick ANY employee and assign him to the drop down. It's a way to temporarily override the position. Please see this[^] So, on load the combo will be loaded from the lookup table using the code. Then when the user picks a new employee via the dialog, that employee is added to the combo and its Id is assigned to the Job.SuperintendantId. So far I can handle all of this. Here's where I get hung up... I think that this should be done via a control. The control would have the label, a combo, and button. Somewhere I would set the code, probably in the XAML. ALso in the XAML the selected EmployeeId from the control would be exposed and bound to the appropriate field on the job, or whatever entity is using employees. By making this a control I can use it all over the app. What I don't understand is how to create this. I'm guessing the selected Id needs to be a DP so I can bind to it. Can I do in XAML? I'm using MVVM. Do I create a view and VM and work with that? I could use some direction. An example would be great. Many thanks
If it's not broken, fix it until it is
Ok, so I finally got my control to work, but I still have one final question. The DP's and other code for the control are in the control's code behind. I still need to pop open a dialog to allow the user to pick an employee. There is no VM. What's the right way to do this? Is it ok to open it from the control's code behind? Thanks
If it's not broken, fix it until it is
-
Ok, so I finally got my control to work, but I still have one final question. The DP's and other code for the control are in the control's code behind. I still need to pop open a dialog to allow the user to pick an employee. There is no VM. What's the right way to do this? Is it ok to open it from the control's code behind? Thanks
If it's not broken, fix it until it is
If you need info from the user that is required and is simple, then yes, I think it is OK to open it from the code behind. Remember, the separation of concerns in MVVM is to support testing of the VMs and the models independent of the view. So if you need to pop an input box or a file dialog or something from the control's code-behind, then go ahead if that is what makes the most sense. That being said... although I think it is OK to do if that is what it takes to solve the problem, I think you are taking out some technical debt if you do it this way. You may want to reconsider your whole approach a bit. Normally, you would want the view model to react to a change in state in the view. With a control in the view you would normally want it to react to a change in the properties on the VM it is bound to. So generally, you would enter a state whereby a new employee needs picked, the VM might open a window to get the selection from the user. Then you would validate that result (in the VM) and set the property on the VM that your control is bound to so it can display the information. Generally we try to keep the brains out of the controls because that complicates things and makes it harder to maintain. The control should basically be able to take some information and display it. Any logic more than that should generally be in the VM. And as one last thought... does your control need to know anything about the backend data other than being bound to a collection of the available options? Meaning, is there any logic in the control to manipulate the backing data such that there is a dependency on the backing data? Or, in other words, do you have strongly-typed references to data objects in your code behind? If so, that is definitely wrong. (References to members as part of the data template in code or in xaml don't count) HTH.