Hi all i have a listbox MainListBox where i add items to dynamically.
Now i want to navigate to DetialsPage.xaml.cs when i choose an item in the listbox.
where i can then display my info about the selected item.
private void SetListBox()
{
foreach (ToDoItem todo in itemList)
{
MainListBox.Items.Add(todo.ToDoName);
}
}
MainListBox_SelectionChanged ("Generated by visual studio 2010 silverlight for windows 7 phone)
// Handle selection changed on ListBox
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// If selected index is -1 (no selection) do nothing
if (MainListBox.SelectedIndex == -1)
return;
// Navigate to the new page
NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));
// Reset selected index to -1 (no selection)
MainListBox.SelectedIndex = -1;
}
in DetailsPage.xaml.cs is the next method. ("Generated by visual studio 2010 silverlight for windows 7 phone)
I'm aware that the below method does not do what i try.
// When page is navigated to set data context to selected item in list
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string selectedIndex = "";
if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
{
int index = int.Parse(selectedIndex);
DataContext = App.ViewModel.Items[index];
}
}
I would like to access the selectedIndex and call my methods of my object that is in the MainListbox
so Basicly:
Mainlistbox => select item => send that item to details page => details page access the item and call methods on the item (object)
I'm sure this is a basic question tough it seems hard to find any specifics on it. i would like to add that this is my first windows phone 7 app.
There are many ways you can pass an object from page to page:
serialize and deserialize like Dennis said, but this, although feasable, is not practical, unless you want to save the object in isolated storage and retrieve it later.
Place an object in the App.cs class, which is accessible to all pages. Set your object in the master page, retrieve it from the Details page.
Code to put in App.cs: MyObject selectedObject;
Code to put in MasterPage.cs: application.selectedObject = MainListBox.selectedItem;
Code to put in DetailsPage.cs: MyObject selectedObject = application.seletedObject;
You can set the Object in the DataContext of your LayoutRoot, but i don't have the code for that on top of my head.
The answer here is simple - you cannot directly pass an object to another page. You can serialize it to JSON or XML and then deserialize it on the target page, but the serialized item will still have to be passed as a parameter.
Instead of sending the selectedindex as a query string parameter you could send the ID for the object or similar, something that uniquely can identify the object.
Then in the details page you could fetch the correct object from the same datasource that the main list box get its data from (in your case "itemList" which could come from e.g. IsolatedStorage).
If itemList is instantiated and kept only within the main page then you won't be able to fetch the item by ID from the details page. So in that case you'd need to move the itemList to some static or app level storage.
HTH
Related
I am trying to get the item that is currently in the spinner, but not by Selected or OnChange events.
public string GetCurrentSport()
{
string currentSport = spnSports.GetItemAtPosition(0).ToString();
return currentSport;
}
the above code throws and error, I am guessing it is the wrong code to get the Spinner value, this is getting passed to another class from a button click so i can't use the OnSelected Events.
If you need more code, please ask.
A Spinner via its subclassed AdapterView has three methods to obtain the "item" that is currently selected, SelectedItem, SelectedItemId, SelectedItemPosition:
// The data corresponding to the currently selected item, or null if there is nothing selected.
var javaObj = spinner.SelectedItem; // getSelectedItem
//The id corresponding to the currently selected item, or INVALID_ROW_ID if nothing is selected.
var id = spinner.SelectedItemId; // getSelectedItemId
// Return the position of the currently selected item within the adapter's data set
var postion = spinner.SelectedItemPosition; // getSelectedItemPosition
re: https://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItem()
I have a problem with caliburn micro and Longlistselector.
BindableCollection from my viewmodel is bind to ItemSource in LLS in view.
In OnActivate method I load data from local database to my BindableCollection to refresh LLS. From my list page I can go to another page to edit selected item on list, and after save I return back to my list page.
Then again OnActivate method is invoked which fetch all objects of SomeType from local database and assign new BindableCollection so LLS is updated.
This works great when more than 1 element is in local database. But when there is only one object on local database, the LLS is not updated after edit, still presents the same data before edit.
What is more, when I go again to edit this object to edit page, the correct data are displayed.
Below are my OnActivate method
protected override void OnActivate()
{
base.OnActivate();
Task<List<Person>>.Factory.StartNew(() => _service.GetPresons()).ContinueWith(
x => Execute.BeginOnUIThread(() =>
{
this.Persons = new BindableCollection<Person>(x.Result);
}));
}
Any ideas how to fix that ?
Try not recreating the collection on every page activation.
Put this collection init to your constructor
Persons = new BindableCollection<Person>()
and in change the OnActivate method to
protected override void OnActivate()
{
base.OnActivate();
var res = _service.GetPresons();
Persons.Clear();
Persons.AddRange(res);
}
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
SPItem itemBeingAdded = properties.ListItem;
var startTime = itemBeingAdded["Start Time"];
// Some code goes here.
}
I am firing this event while adding an item in a calender list. It is getting fired. No problem. But I am not getting any value from properties. In the above code startTime gives me nothing. Actually, I want to access the column field of the item (properties in my case) is being added.
When the user will click in save button, How can I get the column value in code behind (Inside the ItemAdding(SPItemEventProperties properties) method). Let say, I need Start Time and End Time to compare them with some other values. Problem is in the ItemAdding method. ItemUpdating is working fine.
public override void ItemUpdating(SPItemEventProperties properties)
{
base.ItemUpdating(properties);
SPItem itemBeingAdded = properties.ListItem;
var startTime = itemBeingAdded["Start Time"];
}
Finally, I got my answer from here. The way would be like following
string message = properties.AfterProperties["Description"].ToString();
Another thing is you should use internal name. For my case, display name didn't work.
I have searched around but could not find any references.
How do I delete an item in a generic list that relates to items in a listbox?
I currently have a public static List<Employees> and a listbox named lstRecords, I can remove the item in the listbox just fine, but either everything is removed from the list or nothing at all.
This was my first set of code I was working with:
private void DeleteRecord()
{
if (lstRecords.Items.Count > 0)
{
for (int i = 0; i < lstRecords.Items.Count; i++)
{
if (lstRecords.GetSelected(i) == true)
{
Employees employeeRecord = lstRecords.SelectedItem as Employees;
employee.Remove(employeeRecord);
}
}
lstRecords.Items.Remove(lstRecords.SelectedItem);
}
}
}
This is my 2nd set of code I was working with, I have my List right under partial class, but this is all contained in a method.
private void DeleteRecord()
{
ListBox lstRecords = new ListBox();
List<object> employee = new List<object>();
employee.RemoveAt(lstRecords.SelectedIndex);
lstRecords.Items.RemoveAt(lstRecords.SelectedIndex);
}
So far I haven't gotten either set of code to work the way I would like it to, I'm obviously doing something wrong.
I have a few other blocks of code I played around with but these seemed to be headed in the right direction.
Eventually I'll need to be able to double click an item in the list to pull up the properties menu.
Your code runs fine you just have to make some small changes.
The first code block is Ok however I dont know where your lstRecords are.
But have a look at this just copy the code and run it after you have some records in your employee object.
It's createing a listbox in code then adds it to the form(Winforms) and having the lstRecords globaly.
ListBox lstRecords;
private void IntializeDemoListbox()
{
lstRecords = new ListBox();
this.Controls.Add(lstRecords);
foreach (var item in employee)
{
lstRecords.Items.Add(item);
}
}
And then you will be able to use your first set of code the other set will be like this.
private void DeleteRecord()
{
employee.RemoveAt(lstRecords.SelectedIndex);
lstRecords.Items.RemoveAt(lstRecords.SelectedIndex);
}
What you want to do is bind your ListBox to you List of employees. This post shows the binding and the comments shows the removing code as well. The idea is that when you remove an item from the DataSource, then you won't see it in the ListBox.
Binding Listbox to List<object>
The problem with the DeleteRecord() method is that the lstRecords object you just created isn't the ListBox that is on the form.
So I have two comboBoxes (comboBoxFromAccount and comboBoxToAccount). Each has the same datasource, which is AccountsList (a list of BankAccount objects that was passed from the parent form).
I would like to make it so that if an item is selected in one of the comboBoxes, it would no longer be selectable in the other. The way I'm trying to do this is by copying the list of BankAccounts from the comboBoxFromAccount to the comboBoxTo account, and removing the selected index of comboBoxFromAccount from the comboBoxToAccount.
I think I'm close, but what seems to happen is I have a blank comboBoxToAccount.
Here is my code:
private BankAccountCollection accountsListTransferTo = new BankAccountCollection();
// public property for passing collection data to the dialog
public BankAccountCollection AccountsList
{
get { return accountsListTransferTo; }
set { accountsListTransferTo = value; }
}
// Initial loading
private void TransferFundsDialog_Load(object sender, EventArgs e)
{
textBoxAmount.Text = String.Empty;
textBoxAmount.Select();
comboBoxFromAccount.DataSource = AccountsList;
accountsListTransferTo.AddRange(AccountsList); // Copy content
accountsListTransferTo.Remove(comboBoxFromAccount.SelectedItem as BankAccount); // Remove item
comboBoxToAccount.DataSource = accountsListTransferTo; // Data binding
}
private void comboBoxFromAccount_SelectedIndexChanged(object sender, EventArgs e)
{
accountsListTransferTo.Clear(); // Clear list, if you don't to it, AddRange will just add more items.
accountsListTransferTo.AddRange(AccountsList); // Copy ALL accounts
accountsListTransferTo.Remove(comboBoxFromAccount.SelectedItem as BankAccount); // Remove selected, so user cannot transfer to same account
// Refresh data binding
comboBoxToAccount.DataSource = null;
comboBoxToAccount.DataSource = accountsListTransferTo;
// Select very first item in "TO" combobox
comboBoxToAccount.SelectedIndex = 0;
}
Help would be appreciated.
Try removing the line
comboBoxToAccount.DataSource = null;
I have a vague recollection about comboboxes having problems with this.
Another possible problem that I can see is that you are using accountsListTransferTo both as your master collection and the one where you are removing the selected account from. Every time comboBoxFromAccount_SelectedIndexChanged is called one more account will disappear from the collection (and therefore from the available options in comboBoxToAccount).
I think I have seen comboboxes behave in a way where the SelectedIndexChanged (or a similar) event is triggered as new items are being added. If that is the case here it will explain the empty comboBoxToAccount, because comboBoxFromAccount_SelectedIndexChanged will run once for each bank account being added, essentially removing them from the master list and then rebinding the reduced list. You can easily verify this with a break point in your event handler.