Selecting ListBox item returns null - c#

I'm populating my list using data binding in WPF and everything is working fine. But I can't get the string of the selected item in my ListBox.
Here's my code of the button where I'm trying to get the value of selected Item.
private void hexButton_Click(object sender, RoutedEventArgs e)
{
if (imeiListBox.SelectedIndex == -1)
{
MessageBox.Show("Select IMEi from IMEI List!");
}
else
{
ListBoxItem myselectedItem= imeiListBox.SelectedItem as ListBoxItem;
string text = myselectedItem.ToString();
}
}
And here's my XAML code of the ListBox.
<ListBox x:Name="imeiListBox"
ItemsSource="{Binding Path=Devices}"
HorizontalAlignment="Left"
SelectionChanged="imeiListBox_SelectionChanged" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Imei}"/>
</DataTemplate>
</ListBox.ItemTemplate>
The problem is that string text = myselectedItem.ToString(); is returning null. How does one resolve that?

The imeiListBox.SelectedItem will be an object with the same type as the items you put in the ItemsSource of your ListBox, probably a Device object looking at your code.
You have to cast it like
imeiListBox.SelectedItem as Device;
instead.

SelectedItem doesn't return a ListBoxItem. It returns an instance of the type (Device ?) where the Imei property is defined.
So you should cast to this type:
var myselectedItem= imeiListBox.SelectedItem as Device;
if (myselectedItem != null)
string text = myselectedItem.Imei.ToString();
Or you could use the dynamic keyword:
dynamic myselectedItem= imeiListBox.SelectedItem;
string text = myselectedItem.Imei?.ToString();
Note that this will fail at runtime if SelectedItem returns anything else than an object with an Imei property. If you know the type, casting is preferable.

Related

Select ListView GroupStyle Items

I have a ListView with GroupStyle in UWP like in the example here. I have the following HeaderTemplate:
<GroupStyle.HeaderTemplate>
<DataTemplate x:DataType="data:GroupInfoList">
<Grid Tapped="Header_Tapped">
<TextBlock Text="{x:Bind Key}" />
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
and I can get the selected Key by
private void Header_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
var selItem = (sender as Grid).DataContext as GroupInfoList;
var selKey = selItem.Key;
}
Now my problem is that despite knowing the selected Key, I cannot access the Items from it. In Debug, I can see the Count Property and it is equal to the number of elements, that are inside the Group, but I do not know, how to iterate through it.
I would very much like to iterate through all Items, that have the same Key as selKey and set a boolean property called _isVisible for all those items. What is a good/fast/effective way to accomplish this?
GroupInfoList is derived from List<object>. It contains all the Contact with the same Key. When you get selItem, you can use following code to iterate through it like iterate through a List.
foreach (var item in selItem)
{
var contact = item as Contact;
//suppose you have add a _isVisible property in Contact
contact._isVisible = true;
}

SelectionChanged Combobox WPF

I want to populate a ComboBox based on selection of other ComboBox.
Both combo boxes are populate from database using WCF.
My problem is that on first selection it's not working (just after second selection it's work and it's show results from first selection).
XAML
<ComboBox
x:Name="selClientCombo"
SelectionChanged="listEchipamente"
IsEditable="True"
SelectedIndex="-1"
HorizontalAlignment="Left"
Margin="455,35,0,0"
VerticalAlignment="Top"
Width="215"
ItemsSource="{Binding Mode=OneWay}"/>
<ComboBox
x:Name="selEchipamentCombo"
HorizontalAlignment="Left"
Margin="457,65,0,0"
VerticalAlignment="Top"
Width="213"
ItemsSource="{Binding}"/>
code
private void listEchipamente(object sender, SelectionChangedEventArgs e)
{
List<string> echipamenteWCF = client.getEchipament(selClientCombo.Text).ToList();
MessageBox.Show(" Client Selected !");
if (selEchipamentCombo.Items.Count >0)
{
selEchipamentCombo.Items.Clear();
}
for (int i = 0; i < echipamenteWCF.Count(); i++)
{
selEchipamentCombo.Items.Add(echipamenteWCF[i]);
}
}
At the time SelectionChanged is fired, the Text has not been updated (and hence it holds the previous value).
You should access the underlying data item to get the Text instead:
if(selClientCombo.SelectedItem == null) return;
List<string> echipamenteWCF =
client.getEchipament(selClientComo.SelectedItem.ToString()).ToList();
...
I supposed the ToString() will resolve the display Text. You can always cast SelectedItem to the actual type and access its string property (being shown as Text) easily. You can also access the SelectedValue with condition that some SelectedValuePath is set for the ComboBox.

How can I get LongListSelector SelectedItem as a string in WP8

I have a longlistselector like the below image. now I wanna get the text of the item user's tapped. I've searched a lot but no solution found ;(
pay attention to the image please to give a sample code
http://amiryari.persiangig.com/image/stackoverflow-question.jpg
1) Wire up the SelectionChanged event on the LongListSelector control:
<phone:LongListSelector ItemsSource="{Binding MyListItems}"
SelectionChanged="LongListSelector_SelectionChanged">
2) Retrieve the selected item from the AddedItems collection in the SelectionChangedEventArgs:
private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
var item = e.AddedItems[0];
}
}
3) If your item is an object, and the text is displayed through a property, then you would have access to the text through the property on your object:
MyListItemObject item = e.AddedItems[0] as MyListItemObject;
MessageBox.Show(item.FullName);
If your list is bound to a list of strings, then it would simply be the first item in the AddedItems collection:
string fullName = e.AddedItems[0].ToString();
MessageBox.Show(fullName);
You can always listen for the SelectionChanged event and obtain the string. There is another way if you are using a DataTemplate to style your items in the list. Declare Tapped event in DataTemplate like this:
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ContactImage}"/>
<TextBlock x:Name="NameTextBlock" Text="{Binding ContactName}" Tapped="NameTextBlock_Tapped"/>
</StackPanel>
</DataTemplate/>
Now in our code:
private void LongListSelector_SelectionChanged(object sender, BlahBlah e)
{
var tb = sender as Textblock;
string cName = tb.Text; //This is the string you wanted.
MessageBox.Show(cName);
}

Setting Label object as per Listbox selected item

I need to bind a Label to two ListBoxes. In order to do this I have set the SelectionChanged property of both ListBoxes to the same function:
<ListBox Name="ListBox1" SelectionChanged="UpdateSelectedItem" />
<ListBox Name="ListBox2" SelectionChanged="UpdateSelectedItem" />
<Label Name="DetailsLabel" DataContent="DefinedElsewhere" />
However I am having trouble finding what the selected item actually is. I have gone through all the properties of the sending object and the SelectionChangedEventArgs but I cannot find it. The ListBox is bound to an ObservableCollection of objects, and I would like the Label to display the properties of the last selected item, no matter from which ListBox it was selected. How do I find that?
private void UpdateSelectedItem(object sender, SelectionChangedEventArgs e)
{
DetailsLabel.Content = ???;
}
You can read the selected item text doing something like:
ListBoxItem item = ((ListBox)sender).SelectedItem as ListBoxItem;
String itemText = (item != null) ? item.Content.ToString() : String.Empty;
You have to cast the SelectedItem property to the type of object you have in the list.
In this example I've used ListBoxItem.

Getting selected item string from bound ListBox

I'm having a problem with getting a string from a bound textblock within a listbox, when I use the code below, I can bind the listbox and the listbox has items showing up, but when the item in the list is clicked I don't get the proper string, I print a message box a message with objects names like
"MyApp.Item"
shows up instead. myApp is the name of the app and Item is the name of my model that I am binding to the listbox. The proper text from the selected item showed up when the listbox was not binded.
private void listBoxtrend_Tap(object sender, GestureEventArgs e)
{
selectedText = "";
selectedText = listBox.SelectedValue.ToString();
MessageBox.Show(selectedText);
}
xml
<ListBox ItemsSource="{Binding Item}" Foreground="RoyalBlue"
Height="395" HorizontalAlignment="Center"
Margin="12,111,0,0" Name="listBox"
VerticalAlignment="Top" Width="438"
TabIndex="10" Tap="listBox_Tap" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock TextWrapping="Wrap" FontSize="26" HorizontalAlignment="Left"
Name="tblItem" Text="{Binding ItemString}"
VerticalAlignment="Top" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I'd really appreciate if you could help me thanks
You're binding to the ItemString in the DataTemplate's TextBlock and the Item Collection in the ListView. As such the SelectedValue will be of the Item type. You should actually be doing something like this in your Tap handler to get at the ItemString's value...
private void listBoxtrend_Tap(object sender, GestureEventArgs e)
{
selectedText = "";
var selected = listBox.SelectedValue as Item;
selectedText = selected.ItemString;
MessageBox.Show(selectedText);
}
In your example, the ToString is printing the name of the class. You could also override ToString in your Item model to be whatever you want the string to be.
Note: the types and such may be a bit off, I guessed a bit based off of what you wrote in your question. Also, there is no need to set selectedText to an empty string that will just be overwritten in the third line above. I wanted to keep it so you could get some idea of what I changed in your code.
It's very simple, try following:
string selectedText = ListBox.GetItemText(ListBox.SelectedItem);
You need to also set the SelectedItem of the Listbox to something.
SelectedItem = {Binding SelectedItem}
and rename your ItemsSource to "Items" as that makes more sense.
Your SelectedItem in your codebehind or your ViewModel should then contain a property:
public class Item
{
public string ItemString { get;set; }
}
Try This...
string ListBoxConent = ((ListBoxItem)listbox.SelectedItem).Content.ToString();
Try
listBox.SelectedItem.ToString()
If a property isn't specified in ValueMember then SelectedValue returns the results of the ToString method of the object.

Categories

Resources