WP7 Get listbox item text - windows-phone-7

Is there a way to get the text of a clicked listbox item?
So on click it sets a string to the text in the list box item.

In a new DataBound App, changed the following method to see 3 ways of getting this:
// 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;
var string1 = ((sender as ListBox).SelectedItem as ItemViewModel).LineOne;
var string2 = (MainListBox.SelectedItem as ItemViewModel).LineOne;
var string3 = (e.AddedItems[0] as ItemViewModel).LineOne;
// 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;
}

Related

Xamarin - How to know what has been clicked in collection view?

I have a CollectionView with an image and a button in it. I use following code to see if somebody pressed anywhere within the cell:
private void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (((CollectionView)sender).SelectedItem != null)
{
var item = (picdata)e.CurrentSelection.FirstOrDefault();
((CollectionView)sender).SelectedItem = null;
if (allowfullscreen == "1" || allowfullscreen == "true")
{
Navigation.PushAsync(new Picture());
}
}
}
But how can I know if he clicked the button inside the cell? I was trying to do it via the Click event, but then I do not know which one of all the buttons has been clicked.
you can get the item from the BindingContext of the sender
var item = (picdata)(Button)sender.BindingContext;

My drag n Drop event cause doubling items with clicking?

I did a listbox with drag & drop items
Drag and drop is working perfectly
but if I click only any item it adds it to the list
I did not try changing drag threshold since I didn't know how to rach and change SystemParameterInfo.
private void listBox1_MouseDown (object sender, MouseEventArgs e) {
if (listBox1.Items.Count == 0) return;
int index = listBox1.IndexFromPoint(e.X, e.Y);
string s = listBox1.Items[index].ToString();
DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.All);
if (dde1 == DragDropEffects.All) {
listBox1.Items.RemoveAt(listBox1.IndexFromPoint(e.X, e.Y));
}
}

windows phone listbox

1) This is my code for listbox2 selectionchanged
void PrintText2(object sender, SelectionChangedEventArgs args)
{
if (null != listBox2.SelectedItem)
{
ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
textBlock4.Text = lbi.Content.ToString();
}
}
2) This my code for listbox1 selecionchanged
void PrintText1(object sender, SelectionChangedEventArgs args)
{
if (null != listBox1.SelectedItem)
{
ListBoxItem l = ((sender as ListBox).SelectedItem as ListBoxItem);
textBlock6.Text = l.Content.ToString();
if (textBlock6.Text == "Angle")
{
loadlistAngle();
}
}
}
3)
void loadlistAngle()
{
listBox2.Items.Clear();
listBox2.Items.Add("Radian");
listBox2.Items.Add("Degree");
}
4) listbox1 contains static item "Angle" and on selection of "Angle" at runtime,Angle gets loaded in textBolck6 and then new items "radian" and " degree" gets added to listbox2
5) after this when I click "radian" of listbox2 ,the "radian value doesn't get loaded into textblock4 ,it gives "NullReferenceException" in "lbi.Content.ToString()"
6) how do I modify my code so that at runtime "radian" value get loaded in textblock4 and new items generated will get selected in listbox2
Run your code in the debugger after adjusting it the following way:
Where you have textBlock4.Text = lbi.Content.ToString(); replace it with:
object lbiContent = lbi.Content;
if(lbiContent != null) textBlock4.Text = lbiContent.ToString();
Put a break point at the object line. This way you will know what exactly is the contents of your listboxitem, and if it is null.
Most likely you are just placing something wrong in
listBox2.Items.Clear();
listBox2.Items.Add("Radian");
listBox2.Items.Add("Degree");
Other then that, everything is correct in the code you have provided.
Update:
Also, try substituting
ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
with
ListBoxItem lbi = ((sender as ListBox).SelectedItem;
You don't need to do a double cast.
And the Selected item of the list box may still be empty.
Update 2:
Most likely this shoud be how you retrieve the selected item:
ListBoxItem lbi = (args.AddedItems[0] as ListBoxItem);

how can I add a special first item in wp7 listbox?

The listbox has a datatemplate, but I want add a special item to the listbox, which can not be processed by the datatemplate. How can I do this?
If you have the CompositeCollection at your disposal (- sorry if you don't -) you could try to work with that.
private void ListBox_Loaded(object sender, RoutedEventArgs e)
{
var specialItem = new ListBoxItem()
{
Content = "Very special item.",
FontWeight = FontWeights.Bold
};
var collectionContainer = new CollectionContainer()
{
Collection = MyCollection
};
var composite = new CompositeCollection();
composite.Add(specialItem);
composite.Add(collectionContainer);
(sender as ListBox).ItemsSource = composite;
}
If your Listbox has a reasonable number of items, you can set the AlternationCount of the Listbox to something greater than your item count, and use a DataTrigger on the ListBoxItem's AlternationIndex to do something special such as switching templates if it's equal to 0

Selecting First Item in the ListBox on CollectionViewSource.Filter

My ListBox is Binded with CollectionView Source. When I am Changing the Filter it is Automatically selecting the First Item in the Listox.
App.ViewModel.TasksViewSource.Filter += new System.Windows.Data.FilterEventHandler(Tasks_Filter);
void Tasks_Filter(object sender, System.Windows.Data.FilterEventArgs e)
{
if (e.Item == null)
return;
Task task = e.Item as Task;
e.Accepted = task.Id.Equals(TaskId);
}
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (TasksListBox.SelectedIndex == -1)
return;
Task selectedTask = App.ViewModel.AllTasks[TasksListBox.SelectedIndex];
TasksListBox.SelectedIndex = -1;
NavigationService.Navigate(new Uri("/Views/TaskDetailsPage.xaml?taskId=" + selectedTask.Id, UriKind.Relative));
}
Please Help Me.
Set your ListBox IsSynchronizedWithCurrentItem="False".
What really do you want ?
I you do not want the first item being selected when changing filter, you have first to create a private Task object (and/or a SelectedTask property implementing INotifyPropertyChanged).
On the SelectionChanged event of your listbox, set the SelectedTask with the current selected Task.
Then, after having applied your filter, bind the SelectedItem property to SelectedTask.

Resources