I am having trouble trying to get a click event to fire after the command gets executed. I am trying to follow the MVVM style of development.
Here is the scenario:
In my application I have a grid of 2 by N buttons, where N is a dynamically loaded set of buttons (sometimes it'll be 2 x 2, sometimes its 5 x 9, I don't know). Initially, all the columns of buttons will be hidden except for the first column. Upon clicking the first column, a Command gets executed, and a fade in animation gets played and shows certain buttons from the second column, depending on which button was clicked in the first column. For example, clicking the top button in the first column will reveal the first four buttons in the second column, whereas the bottom button in the first column will reveal the first three buttons in the second column.
Here's the problem:
For each button, I set a CommandParameter to each of the buttons in the first column. When the command gets executed, data gets processed, and a property is set in the VM that says which button was pressed (I just use the CommandParameter again). While the data is being processed, I want to display the next set of buttons to the user. I have a click event that takes the CommandParameter from the VM and plays an animation to show the proper buttons.
However, the problem is that WPF likes to do the Click event first before the Command gets fired. This is a problem since the property in the VM that says which button was clicked doesn't get set!
So that brings me to a couple questions...
1) Is there a way to execute the command before firing the event?
2) Am I doing it right? :P
Instead of using a click event handler, you could just setup DataTriggers on your buttons.
They could respond to the property in your ViewModel, hiding buttons you don't want displayed, and showing the button you do.
The nice thing about this is that your View can be done completely in XAML, and the ViewModel is just setting state (ie: I'm at this state), and the View will automatically stay updated since the binding will cause the new DataTrigger to fire.
Related
I have a data grid that is created dynamically to display information retrieved from a database. The data grid consists of multiple bound columns and two button columns for each row, a view and a delete button.
Clicking either button calls the Page_Load() method with Page.IsPostBack as false.
if (!Page.IsPostBack)
{
this.bindForm(); // Populates drop down lists that the user can select
setAccess(false); // Sets access for the page
// Stuff commented out to avoid confusion
}
else
{
bindDynamicGrids(); // Create the data grid(s) and populate them
}
So when I first click the button the above code gets called with IsPostBack false, and the dynamic grids get bound. But, the button click event does not fire. When I click the button a second time, Page_Load is called with IsPostBack false, and after executing bindDynamicGrids() the button click event is fired. I cannot understand the difference between the first and second click.
I have read a few threads;
ASP.NET C#, need to press a button twice to make something happen
http://forums.asp.net/t/1783694.aspx?ASP+NET+Button+needs+to+be+clicked+twice
To attempt to understand the issue, but I must be missing something. From what I am gathering from the second link, a session variable may be getting set in the click event, which is also being set in Page_Load, and this is all an issue with ordering. If that is the case I am not seeing where it is happening.
When it is not a post back the bindForm() method is called, which populates all drop down lists. The edit click event populates those drop downs with the values from the row, but the click event is always a post back and the form has already been bound.
I have also considered having a script automatically double click one of the buttons anytime the user single clicks, but I have not been able to find an "OnClick" property for the column button. Any help that could be provided would be phenomenal.
The thing is, that the button click event happens after the Page_Load event meaning that the filtering does not get applied on the first postback. It has been updated on the second postback and you see the filtering.
You can try to move the code of your page_load event to OnPreRender so the reload happens after the button click event.
for more information look here: Button needs to be clicked twice
and here
i've been searching on internet for the property that says that a normal button was selected or not, i think there must be one because when you click a button, it turns light blue, regardless the mouse is over it or not, and when you click another button, the previous button changes back to normal and the new clicked button is set light blue.
I need it to know which button was just selected and draw a "resizing" square on it, and it gotta last as long as the button remains as the "selected one".
thanks in advance.
What you are looking for is the Focused property. For actually any Control it returns, whether it has input focus or not (so e.g. hitting Enter will cause the button to be clicked as well). Since it sounds like you want to be notified whenever that property changes, you should use the GotFocus and LostFocus events.
You can give a Control focus programmatically by calling Focus.
you can do two things,
you can do it with the events: mouseEnter and MouseLeave,
this events launches when the mouse enters or leaves the visible control area,
also the focused(boolean) property gets and sets the "selected" element
one solution could be to:
private void onElementMousenter(blablabla,sender e);
{
e.Focused=true;
}
and assign the mouseEnter events in all controls to this one so the last element will be the "selected one" until you select another
I'm making a windows form using c# and added 3 buttons. When I double click on any object, it takes me to the code, the specific function that gets called when that object is activated, depending on its type. However, when I double click on the 2nd or 3rd buttons, it takes me to button 1's code instead of opening the function called by double clicking on the button I pressed.
Help?
That happens when all 3 buttons are assigned to the same event. Select the 2 wrong buttons and delete their event in the properties window. Now doubleclicking one of them will create a new code template for it.. You probably had the three all selected when you created the 1st event..
Remove the button click events via the properties window for each button and re-assign them apart.
I have a text box on a Gridview which I'm allowing some data to be edited. When the enter key is hit, the TextChanged event happens, like I'd expect. But when i change the data and click a button, the button effect not happen. I must click the button again to fire button event. I think it happen because the grid didnot lost it focus.
How to make it possible fire the button event just in one click?
You can change your gridview element's IsTabStop property to false to gain that effect.
Also if you want to cycle with Tab you can TabNavigation="Cycle" .
Both changes are in xaml.
Not much we can do to help you here without the code itself but here are couple things you could try to debug this:
Try different browsers – it may be a browser issue
Try some client side debugging – just open console and see if anything happens when you click the button for the first time
The problem like this, I have a groupBox which contains two radio buttons, when I run the form, the first radio button get checked immediately, so I tried the following:
Set the check property for this radio button to false in Load form.
Set the check property for this radio button to false in the form constructor.
Change the tab index property for this radio button, the selection moved to the next radio button in the form.
None of the above worked with me, any suggestions??
You could try setting it to false in the form SHOWN event instead of the form LOAD event as outlined in this question.
As soon as any of the radio buttons get focus it'll be selected, so you need to set initial focus in the form to another control than any of those radio buttons (worst case I suppose you could have a hidden radio button or other control and give that focus, but I'd not recommend it since it looks funny).
The intent of a radio button set is to provide choice between a set of distinctive and exhaustive values.
That means, that at all times, one and only one radio button should be selected.
If this functionality does not suite your application logic, maybe the logic is flawed, or maybe radio-buttons are not the best UI solution.
As mentioned, a radio button group displays its behavior as soon as any of the radios get focus, which can happen even with just tabbing around the form, so basically the behaviour of the form depends on the user behaving nicely.