Bind UIElement to viewModel - c#-4.0

I have a simple view containing a richtextbox and a button. I want to enter text into my RTB and on clicking my button have viewmodel print the RTB.
I have my command set up from the views print button and in my viewmodel have a UIElement property.
My question is how do I bind the RTB directly to my UIElement property in viewModel?
I'm fine with hooking individual properties of the RTB up but what about the whole control?

Not certain how you might accomplish that using databinding, how about just setting the reference manually?
MyControl.Loaded += (s, e) => {
((ViewModel)MyControl.DataContext).UiElementProperty = MyControl;
};
... although I'm not sure why you want to perform a task like that in the VM. How about just handling it in the view? Otherwise you might also encounter "dialogue must be user initiated" type errors.

Related

EditBox fieldInput.getValue() and spaces

Here is the issue. I have an editBox which I am trying to retrieve the value from by server side JavaScript via the onClick event of a button named Add. It works until space is in the value, then it retrieves nothing. Code for the onClick events is as follows:
println("Button Clicked");
try{
var forkNumberInput:com.ibm.xsp.component.xp.XspInputText =
getComponent("forkNumberInput");
var forkNum = forkNumberInput.getValue();
viewScope.ForkNum = forkNum;
println(forkNum);
} catch(e){
println( Error in Add button: " + e.toString());
}
When a space is in the text the viewScope doesn't get populated and nothing is written to the server log not even "Button Clicked". No error is written to server log.
If "Button Clicked" is not printed, the likeliest cause is that there are validation errors. Add a Display Errors control to make sure that's not the case.
I second Paul's suggestion to add the Errors control. Besides that: Try to avoid to use getComponent() when all you want to do is grabbing the value. The idea of JSF, extending to XPages, is "data binding". Controls are bound to some data source for their value and you interact with those. getComponent() is meant to be used when you need to manipulate anything else.
So you would go and bind your control that scope variable viewScope.forkNum and you are done. Your button then grabs the value from there and does what it needs to do.
So in summary: Controls want to be bound. Data lives in models (not controls)
Thanks Paul - you led me to the right direction. It turned out that there was a validateConstraint under Properties > data > validators. I removed the validateConstraint and it started working. That's what happens when you copy design elements from one custom control to another.

How to pass var variable to custom control

I want to create a generic action bar custom control with Save, Edit, Delete, ... buttons.
How can I pass var variable from xpage to a custom control?
Update
I successfully transferred document object to custom control and I can Save the changes made in document, but I can't delete it with same object.
Update:
<xp:this.action>
<xp:executeScript
   script="#{javascript:compositeData.datasrc.save()}">
</xp:executeScript>
</xp:this.action>
Delete is not working:
<xp:deleteDocument
message="Do you want to delete?"
var="#{javascript:compositeData.datasrc}">
<xp:this.name><![CDATA[#{javascript:var page = sessionScope.get("prevview");
return (page=='')?'home.xsp':page}]]> </xp:this.name>
</xp:deleteDocument>
I tried also with:
var="#{javascript:compositeData.datasrc.getDocument()}">
but also didn't work.
When you define a custom control, you can specify control properties. These properties then show up in the property editor when you insert the custom control into an XPage or another control. You can specify the data type and allow them to repeat.
This is saver than to rely on scoped variables. Check Chris' introduction and the XPages 101 session or and many more for inspiration
You can do it for example with a Scoped Variable. If the variable value is specific to that XPage (and user) viewScope is probably the best.
The above options are the best way to do that, but keep in mind that if you define a variable in a scriptblock or somewhere else in the Xpage, you will be able to access this variable from your code in the CustomControl, too. I think that's because the XPage und the custom Controls are kind of merged when compiled. Keep that in mind, this can lead to very nasty problems, especially with recycling issues.
What is the purpose of this variable ? If its a variable to control which buttons are being shown it would be best to create properties for each button / section. These properties can be computed to either return true or false.
If you want to pass the code that a button should execute I would advice you to generate button bars for the most common locations ( aka actions ) and add custom buttons on the button bar by using a facet (Editable area its called in the designer) .On this facet you'll drag a panel on which the buttons are being placed.

Resetting ui: how?

I have an activity with a UI with many elements (Radio, EditText, etc.). When I change the text in an EditText I want that UI back at its starting state.
I tried to kill and restart the activity but with poor results. Any ideas?
Thanks
I would try calling setContentView again with a new view or with the xml file you used for rendering your activity in the first place
Once you get the value you need from your editText, you can reset it using
editText.setText("");
You can similarly programatically control other parts of your UI too.
Why not put all of the reset stuff in one function and simply call it when you need to:
public void resetUI()
{
//reset UI programatically
}
You could add a listener to the EditText view and when the text is what you want it to be you can just programmatically empty any TextView's, EditText's, reset any radiobuttons or radio groups to their default values. No need to restart the activity. Just write a helper method that resets your view "manually". Using setContentView() could also work although I haven't tried it and you might have to setup your complete view again with listeners and such.

LWUIT TextArea doesn't catch touch events

I have made a Container compound by a TextArea and a CheckBox. In my app, I create so many of this "Containers" and add to each TextArea inside of them an ActionListener (implemented by the class). Something like this:
for(int i = 0 ; i<20;i++){
MyContainer c = new MyContainer();
TextArea t = c.getTextArea();
t.addActionListener(this);
}
I want the TextArea to catch the event and if it is pressed put the CheckBox checked or unchecked. It works fine in non-touch devices and simulators but in the touch devices or emulators, the TextArea doesn't catch the event. I tried to put the TextAreaas the lead component of the Container but it doesn't works because Container doesn't have an addActionListener method.
If I understand the question correctly you are trying to create a compound component assembled from multiple different components to act like a single component. In LWUIT/Codename One this is called a Lead Component, the attached post is mostly about the resource editor but the concepts apply to manual coding as well.
Just set the "checkbox" as your lead and everything should work.
You can code this manually by deriving and overriding but you will there are small edge cases like the change of style states (focused/pressed state etc.)
It is better that you derive the TextArea class for the getTextArea() method. Then in this class implement the pointerReleased method : code the action performed in the normal way in it.

How can I get access to items of a modal dialog in MFC?

I just want to get access to an object in the modal dialog. The following example will explain exactly what I'm trying to do:
(This code is not working)
CAddDlg dlg;
CString S;
dlg.DoModal();
dlg.GetDlgItem(IDC_NAME)->GetWindowTextW(S);
MessageBox(S);
But an assert will fail and I can't get the text of the Edit control.
What should I do?
You can't access the controls of modal dialogs from outside. Even if you could, it's not a good idea. The caller of the dialog should not know how the data is represented in the dialog. What is now an edit control could be a listbox in the future.
The way to go is declare getter functions which you call after DoModal() (if it returned IDOK) and get the values there.
Check Can I return a custom value from a dialog box's DoModal function? for some examples

Resources