Is there a best practice for enable (CanExecute) a button in an other userControl? - catel

Is there a best practice for how to enable (CanExecute) a button in an other userControl?
A simple scenario is as follow.
When selecting one row in a datagrid in UserControl_1 a button should be enabled in UserControl_2 and also provide the selected row to UserControl_2's viewModel.
When the user then push the button in UserControl_2 it will be executed with the data from the selected row from UserControl_1.
Thanks from a Catel newbie.

If you are using WPF (assuming you are), the CanExecute will be triggered by the WPF system (CommandManager to be precise). Catel should automatically requery the commands when a property changes.
If the logic (or at least the data) for the CanExecute should be shared among several user controls (and I am talking about the view models for the several user controls), I recommend to create a service which is registered in the ServiceLocator. In the CanExecute of the commands you can query the same data (the service) and return true or false according to the logic you want to implement.

You may take a look at this post. And you 'll have a good idea about your problem. I don't post the code here since it is an answer of someone else. Please refer the link.
http://social.msdn.microsoft.com/Forums/vstudio/en-US/198c4a4b-b4c2-4dfc-b99b-afb2a11eb4c3/call-button-even-from-user-control-to-another-user-control

Related

Catel Edit Master-Detail View multiple times

I'm trying to edit a Master-Detail View multiple times in Catel.
My question is, if there is no common way in catel to handle a Master-Detail-View and multiple editions using the SaveViewModelAsync and CancelViewModelAsync methods?
The workflow works when my Master-ListItem is no UserControl with it's own ViewModel and when I change the selected Master-ListItem after save or cancel, so that a new ViewModel will be created. But I don't want to null or change the selection after a save/cancel. Also I have maybe to create a UserControl + ViewModel for the Master-ListItems.
Restrictions of the question:
I have got the information from Geert van Horrik from the discussion of his answer, that
multiple edit cycles are not supported directly
one Model should only be accesd by one ViewModel
Problem:
I can use the the SaveViewModelAsync and CancelViewModelAsync only once. After that, the ViewModels which are alive
will get no updates from the Model
don't create a BackUp from the Data, so that the changes can be reverted again
Code to Reproduce:
I have created a WPF Project, where I tested this all.
Possible solution:
I could use the EditableObjectHelper and handle this by myself
I have to instantiate the VewModel again, after the save or cancel, like it happends on a selection change in my Example Code
Restricion of the solution:
If I use a specialized UserControl for the ListItems of the Master-View, 2 ViewModels are looking on 1 Model. After the SaveViewModelAsync the ViewModel doesn't get any Notifications of the Model. So I would have to instantiate this ViewModel again too. But I would Breake the 1-1 relation of the ViewModel-Model.
My conclusion:
It seems, that I have to instantiate all these ViweModels and handle this workflow all by myself. It seems, that the ViewModelLifetimeManagement.PartlyManual (CloseViewModelOnUnloaded is Obsolete) doesn't work here. On selection change, always a new ViewModel will be created, the old one will probably just not be closed.
I don't want to misuse the framework. Maybe I have missed something. I hope somebody can help me or give me a tip, how to handle this.
Catel calls Save / Cancel as soon as the view model get's unloaded. If you want to do "intermediate" saves without changing the data model, you can:
Save the master list which will save the dirty models (you can directly modify the models from within your vm's)
Create a custom command (SaveData) that you can run from an explicit button (or input gesture binding) to save the data without calling SaveAsync on the vm
After the answer of Geert van Horrik, I currently ended up , don't using the SaveViewModelAsync and CancelViewModelAsync methods and handle the save and cancel by my own, so that the notification will not breake. I'm using the GetChildViewModels() method on the ViewModelBase and the EditableObjectHelper.CancelEditObject(Model);

How can I activate (display) a view using Revit API?

I am trying to activate a view using Revit API. What I want to do exactly is to prompt the user to select some walls, but when the user is asked that, he can't switch views to select more walls (everything is greyed out at that point).
So the view I want to activate (by that I mean, I want this view to be actually shown on screen) already exist, and I can access its Id.
I have seen threads about creating, browsing, filtering views, but nothing on activating it... It's a Floor Plan view.
So far I can access its associated ViewPlan object, and associated parameters (name, Id, ..).
Is it possible to do ?
Thanks a lot !
Arnaud.
I think the most preferred way is the UIDocument.RequestViewChange() method. The tricky part about this is that unless you've designed your application to be modeless with external events or idling, it may not actually happen until later when control returns back to Revit from your addin.
(There's also setting the UIDocument.ActiveView property - not positive if this has different constraints).
The other way that I have done it historically is through the use of the UIDocument.ShowElements() command. The trick here is that you don't have control of the exact view - but if you can figure out the elements that appear only in that view, you can generally make it happen (even if you have to do a separate query to get a bunch of elements that are only in the given floorplan view).
Good Luck!
I think the solution to your problem may be:
commandData.Application.ActiveUIDocument.ActiveView = View;
The ActiveView is a property and it has {get and set} options.
ActiveView has only a get accessor, what Mostafa suggests will not work.
I have used the RequestViewChange() method with a modal dialog and have not had problems so far.

Create domino view dynamically in XPages

I want to know if I can click a button in my XPage and dynamically create a Domino View and then show it in a panel control on the same page. The reason I want to do this is because I have a categorized view and I don't want to lose category data by using full text search. So I am thinking of creating a new view dynamically and pass my search parameters, like end date or start date, into the view selection formula.
Is it possible? Any other alternative solution is also welcome.
yes you can, but you don't want to. A Domino view takes space in the database and quite some time for its first use. So you end up with a lot of views taking space and the need to adjust database space after removal. Your response times will suck big time.
Categories as shown in Notes views are no web interaction pattern, so you might want to solve a problem that actually shouldn't exist.
The preferred method for Domino application is navigation / drill down over search. But you could do a FTSearch where you add your category to the search parameters and render your results in a repeat control instead of a view control. There you have more control over the look and feel.
Whether or not it's the best solution, the answer to the immediate question about creating a view on the fly is yes: the Database class has a couple "createView" methods to allow you to create a new view, either entirely from scratch or based on a named other view. From there, you can use the "setSelectionFormula" and "createColumn" methods in the created View to build what you want. You can't do EVERYTHING with those methods, but it may be enough.
One problem you'd likely run into is ACL access: you'll need Designer rights to the database, which a normal user most likely wouldn't have. If you use the sessionAsSigner object to fetch a signer version of the DB (say, "var signerDB = sessionAsSigner.getDatabase(database.getServer(), database.getFilePath())"), you can work from there. Off the top of my head, I don't remember if you will also have to up the "Maximum Internet access" setting on the last tab of the ACL to Designer as well, but you may.
I am assuming that you are referring to the problem that exists when you choose the documents based on the category. This is something that I find highly annoying and I wish that it was possible to turn this on and off. It makes sense for embedded views, but not for much else.
What I did to solve this was to include the category value in the next column. In this way that text could still be seen, even if it was a flat view.
Alternatively, you could also look into using a repeater control and create your own way of presenting the information. This would be used instead of a (Dynamic)ViewPanel control. You could then present the information any way you wanted as long as it is returned in the viewrow set.
Happy Programming!

How should I implement `OnLoad` event handler when creating an activity using a contact?

I've created my own solution with a custom entity of type activity. I'd like to show a message whenever a new instance of it is created using an existing contact but not allow the user to create one, if only attempting to do that without going via contact.
Basically, my aim is that it won't be impossible to just create that activity (the form will be hidden directly at any attempt except for one way only). The user will have to go to contacts (or leads etc.) and in there add and create an instance of the custom activity entity. That way, I can assure that the field "regarding" will be filled out already.
I'm guessing that I need to detect somehow that the opening of the form is a creation attempt. How can I do that?
Also, as it is now, the user can't create a contact-less activity of the custom type because it doesn't appear on the menu with other activities. I must have disabled it somehow but I have no idea how. Anybody who has one?
You could do this a bunch of ways but the easiest would probably be to:
Make the regarding field read only.
Make the regarding field mandatory.
That way if a user opens a create new form they wont be able to set the regarding and because its mandatory they wont be able to save the record. When they open via an existing contact the regarding field will be mapped automatically. That said in this case just making it mandatory my be enough.
(As a side JavaScript can be used to identify the current form state, but I'm not sure how useful that is here).
In terms of where custom activities appear, by default mine show in a number of locations, for example:
CRM > Workplace > Activities > Ribbon > Other Activities > XXX.
CRM > Workplace > Activities > View Selector > XXX.
They don't show under the left hand navigation of the workplace because they are grouped under 'Activities'. I'm pretty sure these are all the default settings.
You can exercise greater control by editing the sitemap, where you can put pretty much anything, anywhere.
In addition to Mr Wood, I'd like to show you some code. It works as supposed to but I'm not sure if it's optimal.
var foo = function () {
var whatIsGoingOn = Xrm.Page.ui.getFormType();
if (whatIsGoingOn === 1)
alert("Let there be an entity!");
else
alert("Not a creation...");
}
The other states' (deletion, update etc.) numeric values are listed here.
Answering the second part of your question:
When you create a custom activity you can choose whether to have it appear in 'normal' Activity menus or not by checking the box at the top right of the entity form. This is a once-only choice as far as I know and can't be changed later.
For your setup, I would suggest NOT checking this box, so it does not appear in the activity menus to avoid users even being tempted to do it that way.
Instead, add an explicit relationship to the activity N:1 to Contact, and another N:1 to Lead. Use this relationship to add your activity to the left navigation of Contact and Lead forms, or add a grid for them (depends on how you want to use this and if you need to filter the view to something other than the default "Associated View").
When a user navigates to this section they will see if any previous activities of this type exist, and be able to add a new one. BUT this means that the child record is a child via this relationship, not using "regarding", so use a script on the form for the activity so that if Contact is filled in, it is copied to Regarding, and if Lead is filled in then that is copied. If neither, then use an alert or other means to warn the use that something is wrong (see comment earlier). If you want Regarding to be read-only but filled in by script, you will need to make sure to use the force the value to be saved:
Xrm.Page.getAttribute("regardingobjectid").setSubmitMode("always");
You must have the lookups for Contact and Lead on the form to be able to use them in your scripts, but you can make them not "visible by default" so they are there but not seen by the user (and taking up no space).

SharePoint 2010 - Customizing the rendering and behavior of a List field

In my SharePoint List, I have an "Employee" column that is a User type field. I would like to add some custom Business Logic to the processing of this field.
Currently, when the user adds a row, I check to see if the user is an Employee or a Manager and then change the behavior on this column accordingly. I do this by statically rendering the field in my custom "ListForm Rendering Template", just before my custom ListFieldIterator. I simply use a standard SharePoint FormField (and FormLabel) control. In the markup of the FormField control, I specify the FieldName (Employee) and an event handler for the Load event. In this Load event, I will check to see if the current user is an Employee or Manager (using two different SharePoint groups). If the user is an Employee I set the value of the field to the current user (this part works perfectly). I also want to change the field so it can't be modified. I thought I might be able to just change the ControlMode on the field (in the code of the OnLoad Event Handler) to Display, but for some reason this has no effect. The field still renders with the full, people picker editor. Am I not changing the fields control mode soon enough? Or is this simply not the correct approach? The other logic I want to put in is if the user is a Manager, I would like to allow that user to select the person from a list (SharePoint group) of Employees. It may be easier to just use the people picker and limit the selectable users to that group. (I think I can do this with the SelectionGroup property.) Although, it would be better if I could just provide a dropdownlist of users, which I could possibly do with a hidden dropdownlist that I would show and event handlers that I could use (handle event selectedindexchanged) to pull the value selected and populate the (now hidden) Employee (user) field. Does this approach make sense? Assuming all that will work, the real difficulty I am having is with changing the ControlMode (rendering) on the field (when the user is an employee) to a label or some kind of read only control, which is how that field renders when viewing the row, which is why I think if I can just trick the control into thinking it is in Display mode then it should work perfectly!
I am still learning SharePoint, but I am very proficient in ASP .Net. This is why I would like to keep my customizations in this Custom Rendering Template, using code behind and leverage my existing skill set as much as properly.
Any thoughts, opinions or advice? Does anyone know why I can't get the column to switch the "Control Mode"?
I do not think that I fully understand your scenario. Some code samples could help.
But anyway it sounds like you want some heavy customizations of the user field. In that case you might want to have a look at creating a custom field with all its advantages and disadvantages. Have a look at MSDN: http://msdn.microsoft.com/en-us/library/gg132914.aspx
Another option might be - in case you do not want to re-use this column in many list definitions - that you can get away with your custom rendering template and create a custom create/edit form where you implement the specific edit behaviour for the field (plain ASP.NET with some SharePoint controls). Here is a nice walk-through on how to grab a custom edit form from SharePoint designer: http://community.bamboosolutions.com/blogs/sharepoint-2010/archive/2011/05/12/sharepoint-2010-cookbook-how-to-create-a-customized-list-edit-form-for-development-in-visual-studio-2010.aspx
I hope this helps. Kr., Bernd.

Resources