I'm developing a WP7 app and have a listbox with generated buttons all supposed to lead to somewhere specific. I can't figure out how to know which button was pushed at runtime. The list gets generated from a collection of objects with a couple of attributes in each. One of those attributes contain a value that I need to get to be able to know where to send the user.
So my desired process is that the user clicks on an item in the listbox, passing the value of the attribute in the object the button was generated from, to a click handler which sends the user to the right place.
Any suggestions?
I presume your ListBox contains a ItemTemplate which constructs a Button for each of the items bound to your list? if this is the case, within your Click event handler you need to inspect the DataContext of the button that was clicked:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
var myObject = btn.DataContext;
}
As an aside, if you are using this for navigation, a ListBox will not give you very good performance. See the following blog post for an alternative:
http://www.scottlogic.co.uk/blog/colin/2011/04/a-fast-loading-windows-phone-7-navigationlist-control/
Check the sender property of the OnClick event handler for the click handling.
Alternatively you may want to handle the SelectionChanged event of the ListBox and then query the contents of the SelectedItem.
Related
I'm working on a form and I need to change the behaviour of some controls when some actions are given by the user. Since there are many controls to handle, meaning buttons, comboboxes, and others, do you know how I can handle methods for all those without handling one by one? Like a loop or something.
Like, for example, for comboboxes I need to overwrite the drawItem event with some code to change their appearance.
I can't find anything on google.
I did it, in the end. I set for the many comboboxes the same event handler/method, selecting all the comboboxes and then setting the handler name in the properties/events tab (in my case, in the DrawItem handler).
Then in the code the method comes by default with (object sender, DrawItemEventArgs e) so I set:
var combo = sender as ComboBox;
and with it I can generally get or set what I need
In my app I have a table with some rows. When I press on a '+' next to row, I need to expand it and display another table. How can I add an event handler to code for this '+'? When I press twice on it, I create event handler for click event for all row and I need it only for a '+' button.
This is what it generates:
private void GridCandidates_Click(object sender, EventArgs e)
{
}
This is how I added '+':
[NotMapped]
public List<CandidateStatusLog> CandidateStatusesLog { get; set; }
Most of the events related to the GridControl are actually fired by the view, not the control. For instance, in this case you can handle the GridView's MasterRowExpanding event which is fired as the [+] is clicked and a detail table is about to be displayed.
If you want to dynamically change the details view at runtime, I would suggest reviewing the Load Details Dynamically by Handling Events documentation as well.
The GridControl should automatically be able to generate a detail view if the parent object has a collection property such as a List like you're doing. See the Binding to Objects with Collection Properties for information about this.
I have created a ListView, but don't know where to start to make clickable each element of the list.
My purpose would be to display in a different hubSection a lyrics of a song, based on the chosen one.
I created the:
SelectionChanged="TestiCanzone_SelectionChanged"
I have a method with this name. should I do there?
No, use ItemClicked to know when the item is clicked. SelectionChanged event is fired when the items are selected or not.
Then in your handler, access ClickedItem property of ItemClickEventArgs arguments.
I have listed a series of questions and each number can be two possible answers, only one of them is that you can select, for such the reason I grouped in a GroupBox and two RadioButton put each GroupBox. The problem is that if they select an option that I want you must add an extra comment for this response, only when you select that specific response. However, there are 25 questions and make each event RadioButton increase for each lot code and not be optimized, there will be some form of that once the change is made response in each GroupBox is generalize so take that GroupBox I was there and verify what that change, ie: answer 1 or answer 2. And the name of GroupBox can work it more practical. Can you? Create an event that is waiting to cambiod and each RadioButton and it takes the response data. Thank you! -
You can use one handler to handle the same event from multiple radio buttons, then inside the handler look at the sender to determine which box was selected.
In circumstances like this, I'll use the tag property of the control to store some information about where I want the data to go back to. In your group box problem, I'd store the tag on the group box:
this.radioButton1.Click += this.radioButtonx_Click;
this.radioButton2.Click += this.radioButtonx_Click;
this.radioButton3.Click += this.radioButtonx_Click;
this.radioButton4.Click += this.radioButtonx_Click;
private void radioButtonx_Click(object sender, EventArgs e)
{
(((sender as RadioButton).Parent as GroupBox).Tag as MyWidget).MyProperty
= (sender as RadioButton).Text
}
The best way to do this would be to make use of ajax and use a div which the comment can be inserted along with the selected radio button and then stored.
Alternatively you can use one method in the bank end to capture that information. You would still have to call it for each individual radio button though.
If this is not what you are attempting to do please clarify.
I am using Telerik RadGrid, i add a new button in the grid but how can i write an event for this button (Buy) for example when the user press (Buy) it will add this item to his cart with its price in order to calculate his bill.
regards
You need to listen for the ItemCommand event:
<telerik:GridButtonColumn UniqueName="Buy" ButtonType="LinkButton"
Text="Buy" ConfirmText="Add to cart?"
OnItemCommand="rg_ItemCommand" CommandName="AddToBasket" />
In your codebehind
protected void rg_ItemCommand(object sender, GridCommandEventArgs e)
{
if(e.CommandName == "AddToBasket")
{
// Add to basket code here
}
}
You may also need to set the CommandArgument during the ItemCreated or ItemDatabound events, or get it using something like rg.MasterTableView.DataKeyValues[e.Item.Index]["ItemId"].ToString(); after setting ClientDataKeyNames="ItemId" in your MasterTableView settings part in the ascx file (if it is databound).
You need to use the ItemCommandEvent of grid. The ItemCommand event is raised when a button is clicked in the Telerik RadGrid control. This allows you to provide an event-handling method that performs a custom routine whenever this event occurs. Follow the LINK to have more details.
When you create the button you'll want to add an OnClick event to it to handle the click on the button. In this event you'll then add the item to the cart. You'll need to parse the parent row of the button to know which item it is.
Edit:
Since you are using a GridButtonColumn and not adding a button as you stated then this applies instead (from Telerik.com):
This column renderes a button of the
specified ButtonType in each
corresponding cell of the items of
type GridDataItem and
GridEditFormItem. You can use this
buttons to fire command events that
can be handeled in RadGrid.ItemCommand
event handler. This, in combination
with the event bubbling mechanism in
Telerik RadGrid, allows you to create
a column of custom button controls,
such as Add, Remove, Select or Edit
buttons.
So essentially you'll need to use the grids ItemCommand event to handle the button click.