Getting selected item string from bound ListBox - c#

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.

Related

Selecting ListBox item returns null

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.

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.

C# WPF Checkbox Databinding

I'm looking for the best way to populate a check boxes from the following code. I have looked into Binding but not really sure where to go.
Here is the edited code that is working
private void dpDateSelection_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
DateTime? date = dpDateSelection.SelectedDate;
logDate = date != null ? date.Value.ToString("yyyy-MM-dd") : null;
dpDateSelection.ToolTip = logDate;
LoadLogs(logDate);
}
private void LoadLogs(string ldate)
{
string[] logs = Directory.GetFiles(logPath + ldate, "*.ininlog");
InitializeComponent();
logList = new ObservableCollection<String>();
logList.Clear();
foreach (string logName in logs)
{
string s = logName.Substring(logName.IndexOf(ldate) + ldate.Length + 1);
int extPos = s.LastIndexOf(".");
s = s.Substring(0, extPos);
logList.Add(s);
}
this.DataContext = this;
}
<ListBox x:Name="Logs" ItemsSource="{Binding logList}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding}" ToolTip="{Binding}" Tag="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
You will want to start by using an ItemsControl instead of a StackPanel, since ItemsControls are automatically set up to display collections of things:
<ItemsControl ItemsSource="{Binding Logs}"/>
Note the use of ItemsSource. With the accompanying binding string, it basically says "Look for a property on the DataContext called "Logs" and put everything in it into this control".
Next you said you wanted this displayed as checkboxes, so we use an item template:
<ItemsControl ItemsSource="{Binding Logs}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content={Binding .}/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
This says "Use a checkbox for each Item in the ItemsSource". The DataTemplate can be a Grid or other collection control as well, so this is a really powerful feature in WPF. The "Binding ." just binds to the object itself (a string in this case, so we don't need a special path).
Finally, you need to set up a property to bind to in your view model:
ObservableCollection<String> Logs {get; set;}
You want an ObservableCollection so that when anything is added to or removed from the list, it automatically updates the UI. If you are going to be completely replacing the list (assignment), then you need to implement INotifyPropertyChanged and invoke the PropertyChanged event in that properties setter.
In your posted loop, you would add each log file to this property.
Also, make sure that somewhere you set the DataContext property of the XAML file (View) to your view model object. If everything is in code behind, use DataContext = this. Note that doing this is considered bad practice, and you should use a separate class (ViewModel).
You didn't mention what you wanted the CheckBoxes to do, so I haven't included anything related to that in my answer. You will likely want to abstract your logs into an object with a "Selected" property you can then bind the IsChecked property of the CheckBoxes to.
Obviously this is a lot to take in, please let me know if I can clarify anything or help further!
Update
You put the property in your ViewModel (DataContext). Whatever class that is, you write:
ObservableCollection<String> Logs {get; set;}
private void LoadLogs()
{
string[] logs = Directory.GetFiles(logPath + logDate, "*.ininlog");
foreach(string logName in logs)
{
string s = logName.Substring(logName.IndexOf(logDate) + logDate.Length + 1);
int extPos = s.LastIndexOf(".");
s = s.Substring(0, extPos);
//MessageBox.Show(s);
Logs.Add(s); //Add the parsed file name to the list
}
}

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.

Categories

Resources