I'm working with Silverlight, how can I force all my textboxes to trim all leading and trailing whitespaces from their Text property in one place, say App.xaml or something else?
I don't want to be setting an event handler for the GotFocus event every time I use a TextBox.
And I want to keep the ability to fully use xaml, for instance, If I create a new control that inherits from TextBox, then I'll loose the xaml ability to set things declaratively.
It could be with behaviors, a global setter, or whatever action that allows me to keep using xaml and affect all textboxes.
You could create a behavior, but if I were you, I would create a new control that inherits from TextBox. You will not "lose xaml ability".
public class DerrivedTextBox : TextBox
{
public DerrivedTextBox() : base()
{
this.TextChanged += DerrivedTextBox_TextChanged;
}
void DerrivedTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
//TODO: Edit text here
}
}
Related
I have an outputtext showing the number of services on the screen:
<h:outputText
value="Services #{bean.counterManager.serviceCounter}">
</h:outputText>
and below it an accordionpanel that calls the getServices() method:
<p:accordionPanel value="#{bean.services}" var="service">
In the getServices() method I increment the counter and when I debugged at the return point it was 143.
public List<Service> getServices()
{
if (this.services.isEmpty())
{
//Does other stuff, fills this.Services
this.counterManager.incrementServiceCounter(someValue); //
}
return this.services;
}
But it appears 0 on screen, because getServices() is called after outputText calls getCounterManager() probably because the outputtext is above the accordionpanel on my XHTML.
I'd like for serviceCounter to show 143 and not 0, but I don't how to make it render after getLinhasStruct() is called, I can't put it the outputtext below accordion panel because that would mess with the layout of the page so how can I do that?
Never do business logic in getters. You need to make sure that all your getters (and setters) are pure getters (and setters).
public List<Service> getServices() {
return services;
}
After your IDE autogenerates them in the very bottom of the bean class, just ignore them forever. Do not touch them. Do as if they do not exist. You're supposed to perform business logic in action event listeners.
Your concrete problem is caused because those getter methods are during render response phase called in the same order as the components appear in the tree, and your code is incorrectly relying on something which is not controllable from inside the backing bean.
You did nowhere state the concrete functional requirement in the question, so it's a bit hard to point out the right approach, but there are in general the following approaches depending on when exactly you'd like to perform the business logic.
During initial GET request? Use <f:viewAction>.
Right before rendering the view? Use <f:event type="preRenderView">.
During bean's initialization? Use #PostConstruct.
See also:
Why JSF calls getters multiple times
What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
When to use f:viewAction / preRenderView versus PostConstruct?
I am working on a MvvmCross based android app. The app contains (among other things) a large number of ToggleButton(s). The buttons are added in .axml files. Their Checked property and Click event are bound to view-model properties. Since the Checked property of the each ToggleButton should reflect the state of some property on a application server, I don't want their checked state to be changed when the user clicks them, but only when the bound property on the view-model is changed. An example how this "special" toggle button should work: When the user clicks it, the "Checked" property of the button does not change only the ICommand to which the click event is bound to is invoked. The method invoked by the command in turn changes the value of the property on the view-model(if executed successfully). Extending a ToggleButton in WPF or Windows Forms to described functionality is easy but I don't know how to do that in android. Any ideas will appreciated.
Uroš
I found the solution to my problem. It seems it had to do something with the way I set up the bindings for my ToggleButton
MyToggleButton was implemented as:
public sealed class MyToggleButton : ToggleButton, View.IOnClickListener
{
public MyToggleButton(Context context, IAttributeSet attrs)
: base(context, attrs)
{
SetOnClickListener(this);
}
public void OnClick(View v)
{
Checked = !Checked;
}
}
As you can see I just set the Checked property back to previous value when ever the user clicks the button. In the asmx I use the following block to add the button.
<controls.MyToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
local:MvxBind="Checked Value;Click ClickCommand" />
As it turns out the problem with described solution is that the OnClick(View v) method is not executed when the user clicks the button as long as the .axms file contains binding to Click event. I have no idea why that is so I would love any explanation. The workaround I used is to define custom event in the derived class, raise the custom event in the OnClick event handler and than bind to that event.
Uros
I'm new to Blackberry Development and i have to implement a search functionality. It is supposed to be like when i input something in the search field and press the search button it should pass that string to a url as a parameter. Can anyone put me in right direction so as i can understand how to implement it.
If you want the searching to be done on a remote server, and just want the user to be able to enter a search term, then you'll first want a manger with two fields, such as a BasicEditField and a ButtonField. Then you'll want to hook up the button field to submit a request when it is clicked (I assume from your description that is http, but it could be anything). If your button field is stored in a buttonField variable, and your edit field is stored in your editField variable, and you have a method named sendRequest that takes a parameter and sends a request to the url, then you might have:
buttonField.setChangeListener(new FieldChangeListener() {
public void fieldChanged (Field field, int context) {
(new Thread() {
public void run() {
sendRequest(editField.getText());
}
}).start();
}
});
This might look a little complicated, but it breaks down pretty easily:
By calling setChangeListener, we ensure that the fieldChanged method will be called whenever the button is pressed
When fieldChanged is called, we will be in the UI thread. We don't want to lock the UI while we make the request, so you should send the request in a new thread.
getText gets the current text that the user has entered, and passes it to your sendRequest method
The sendRequest method should send the request to the url you mentioned. This will differ depending on how old devices you need to support - BlackBerry introduced a new networking API in 5.0 and expanded on it in 6.0, that simplifies network communications significantly.
I'm assuming you'll be parsing some sort of response that holds the search results. In this case, you'll also want a manager (such as a VerticalFieldManager) to store those results. Then you would add a field for every result that you wanted to show. You'll need to be careful to be holding the UI event lock when doing this, however, since it will probably be executing in a background thread. For example, you might have:
public void addResult(String result) {
synchronized(Application.getEventLock()) {
searchResults.add(new LabelField(result));
}
}
This would just show results in a label field, but you could obviously have more complex fields if you wanted more complicated behavior or more complicated UI.
I'm going through some of the MVC3 tutorials, namely the Pluralsight videos, and I'm thinking of how our existing application would function if it were overhauled to ASP.NET MVC (not the plan, but it gives me a frame of reference). We have quite a bit of code that looks something like this in our aspx code-behinds:
if (SomeBooleanCheck){SomeControl.Visible = true;}else {SomeControl.Visible = false;}
Granted that example is greatly simplified, but assuming the boolean logic is fairly complex and assuming multiple things need to happen as part of making the control visible (maybe changing color, size, text, etc.) what's the pattern for doing this in ASP.NET MVC? It seems like you'd have to do that same boolean checking in the view itself, which to me seems kind of ugly. Seems like there has to be a better way and this surely came up on MS's use case list, I'm just not seeing the answer.
The approach you may take will vary greatly depending on the specific scenario. A few options incude:
Doing as you say and adding the conditional in the view
Abstracting the conditional (if it is complex) into your view model so that the lines in the view are still simple (just accessing a preset boolean value on your view model).
Doing this conditional at the route or controller level and calling a different overall view (which may share a layout (razor) or master view (webforms mvc))
You don't mention explicitly how you would render the controls in the conditional. I assume you would be doing a RenderPartial. So the lines themselves in the view would be quite 'small'.
if(myViewModel.ComplexBoolean) // Boolean set in generation of view model
Html.RenderPartial('firstPartial')
else
Html.RenderPartial('secondPartial')
EDIT: If the item you are setting as 'visible' is simply a single control you may just output the control directly e.g.
if(myViewModel.ComplexBoolean) {
Html.DropDownListFor(m => m.Type, Model.Types /* An IEnumerable<SelectListItem>*/, new { #class = "myList" });
}
Additionally if you didn't want to set that 'Model.Types' property (to save a db hit for example) then the conditional could be in the location you create your view model (either the controller or some service/view model repo). The view could then just check for the properties existance instead:
if(Model.Types != null) {
Html.DropDownListFor(m => m.Type, Model.Types /* An IEnumerable<SelectListItem>*/, new { #class = "myList" });
}
If your controls does not use the data found in your View's ViewModel, you can also use Html.RenderAction to call Child Actions. For example, suppose you want to display a different menu to users with different roles. You can call #{Html.RenderAction("Menu", "Account");} in your View, which will call the "Menu" Action in your "Account" controller. Your complex Boolean logic and the logic to formulate your controllers’ settings will reside in the "Account" controller's "Menu" action. The "Menu" action will decide what Partial View/Controller to display.
// This goes in your View (clean single line!)
#{Html.RenderAction("Menu", "Account");}
// This goes in your controller
[ChildActionOnly]
public ActionResult Menu()
{
bool isAdmin = false;
// Your complex boolean logic goes here
// Set your controller settings here
string controllerSettings = ""; // Use class or array for multiple settings
if (isAdmin)
{
return PartialView("~/Views/Account/_AdminMenu.cshtml", controllerSettings);
}
else
{
return PartialView("~/Views/Account/_StandardMenu.cshtml", controllerSettings);
}
}
My List<BusinessObject> has some public properties that I want to bind to columns in a DataGrid. Unfortunately, the names of the public properties are not good and I may not even know what they are until runtime. For this reason, I set AutoGenerateColumns=True and interecept each DataGridAutoGeneratingColumnEvent so I can inspect what it is and either cancel it, hide it, or name the header something else.
It works great but I cannot figure out how to set the Mode=TwoWay so that my INotifyPropertyChanged events get fired once all the columns are generated and somebody edits a cell.
Bonus question:
On navigating up and down the rows of the grid, does the grid's datacontext automatically get set with that row's BusinessObject?
Thanks to this post, I learned that the binding happens on DataGridTextColumn. So the way to set the Mode at runtime is:
1 private void DataGrid1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
2 {
3 DataGridTextColumn tc = (DataGridTextColumn)e.Column;
4 tc.Header = "Custom Header";
5 tc.Binding.Mode = BindingMode.TwoWay;
6 }
Now that I have TwoWay binding, I have to figure out how changes make it back to my BusinessObject.
If the binding is correct your business objects will automatically receive the required updates. To do you binding programmatically you might need a little more code, something like:
...
Binding binding = new Binding("Propertyname");
tc.binding.Mode = BindingMode.TwoWay;
...