How to handle a selected item of LongListMultiSelector? - c#

I want to be able:
to open a mail when the user taps on one item.
and delete multiple emails when the user selects multiple emails
So I choosed LongListMultiSelector.
In built in LongListSelector, I handle the SelectionChanged event like this:
private void mails_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedItem = mailsLongListSelector.SelectedItem as Mail;
if (selectedItem == null)
return;
...
mailsLongListSelector.SelectedItem = null;
}
I want exactly like that functionality in wptoolkit's LongListMultiSelector. like when you select an email to open and read it.
LongListMultiSelector's SelectionChanged occurs when you tap left side of an item and checkboxes appear. this is not what I want.
The Question is:
How can I perform something when the user taps on one item of LongListMultiSelector? thanks.

You could try this. If this is your LongListSelector
<tkit:LongListMultiSelector Name="longlist" SelectionChanged="longlist_SelectionChanged">
<tkit:LongListMultiSelector.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" FontSize="32" Tap="TextBlock_Tap"/>
</DataTemplate>
</tkit:LongListMultiSelector.ItemTemplate>
</tkit:LongListMultiSelector>
and it has an itemtemplate, you can detect a tap on item.
private void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var itemTapped = (sender as FrameworkElement).DataContext as Book;
}
and still have a selection changed
private void longlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}

Once you are using LongListMultiSelector, the SelectionChanged event is fired when item is added or removed. If you want to perform the action regardless item is added/removed, I've managed to do it like this (for a simple string):
private void longlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string selectedItem = String.Empty;
if (e.AddedItems.Count > 0) selectedItem = e.AddedItems[0] as string;
else selectedItem = e.RemovedItems[0] as string;
MessageBox.Show(selectedItem); // do your work
}
It should run while items are selected separately by tapping, but this method will have problems when more items are added/removed at the same time - if you need it, then you should handle this also.

Related

Calling method on SelectionChanged ListBox event with the selected item as parameter

I'm trying to call a method which fills a table with data according to the ListBoxItem that has been selected.
// Setting the ListBoxItems
myListBox.ItemsSource = list;
// Calling the method when the ListBox's selection changes
myListBox.SelectionChanged += LbItem_Select;
The above snippet can't work because the LbItem_Select event handler doesn't get as a parameter the item that is currently selected.
This is the event handler:
private void LbItem_Select(object sender, RoutedEventArgs e)
{
var lbItem = sender as ListBoxItem;
lbItemContent = lbitem.Content.ToString();
// fill the table according to the value of lbItemContent
}
How could I achieve this?
When you work with events, sender is the object related to the event. myListBox.SelectionChanged is an event of the ListBox. So sender is the ListBox, not the item. Try with this:
private void LbItem_Select(object sender, RoutedEventArgs e)
{
var listBox = sender as ListBox;
// Or use myListBox directly if you have the ListBox available here
var item = listBox.SelectedItem;
// Do whatever with the item
}

How do I get ahold of the data that the user selected from a picker in xamarin forms? I want to use it in a switch statements

I don't know how to get the data that the user selected from the picker.
in conflict, which one to use selectedindexchange so it tells me when user changes it's mind or selecteditem which tells me which one did the user selected.
public MainPage()
{
InitializeComponent();
drainquatity();
}
void drainquatity()
{
drain.Items.Add("1");
drain.Items.Add("2");
drain.Items.Add("3");
drain.Items.Add("4");
drain.Items.Add("5");
drain.Items.Add("6");
drain.Items.Add("7");
drain.Items.Add("8");
drain.Items.Add("9");
drain.Items.Add("10");
return;
}
<Picker x:Name="drain" Title="Drain #" Margin="190,-30,50,0"
"SelectedItem="drainx" />
i want to be able to know what the user selected, use that data to pass thru the switch statement.
drain#: pick from 1 to 10
user picks
switch (drainx)
case:1
then call this function to display only 1 entry.
case:2
call this function to display 2 entries
etc...
the Picker docs explain all of this
// get the index/position of the selected item
// -1 means no selection
int ndx = drain.SelectedIndex;
// get the value of the selected item
// null means no selection
string item = (string)drain.SelectedItem;
// set an event handler to fire when an item is selected
<Picker x:Name="drain" Title="Drain #" Margin="190,-30,50,0"
SelectedIndexChanged="ItemSelected" />
protected void ItemSelected(object sender, EventArgs args)
{
}
<Picker x:Name="drain" Title="Drain #" SelectedIndexChanged="Drain_SelectedIndexChanged"/>
public void Drain_SelectedIndexChanged(object sender, EventArgs e)
{
if (drain.SelectedIndex == -1)
{
//Message
}
else
{
string item = (string)drain.SelectedItem;
DisplayAlert (selectedItem, "OK", "OK");
}
}

How get checkbox object from listview in wpf

I'd like to make checkboxlistview in wpf application like this image
)
If I check any item on left listview the items of right one should be checked.
I can't use binding, because It's shared project and I need to add UWP project after finish WPF project.
So I can't make binding items to VeiwModel for this project.
I'd like to catch the click event of left items and set the check property to right items. But I can get the checkbox objects from the right listview.
void OnCheckboxClicked1(object sender, RoutedEventArgs e)
{
var checkBox = sender as CheckBox;
var id = checkBox.Tag;
CheckSubList(checkBox.IsChecked, id.ToString());
}
void OnCheckboxClicked2(object sender, RoutedEventArgs e)
{
}
void CheckSubList(bool? isChecked, string Id)
{
foreach (object listView in LogicalTreeHelper.GetChildren(rightListView as FrameworkElement))
{
Console.WriteLine("OKOK");
//foreach (object checkbox in LogicalTreeHelper.GetChildren(obj as FrameworkElement))
//{
// // Some code
//}
}
}
If anyone have a good idea, please help me. Thanks

How to link 2 comboboxes

I have 2 comboboxes each with 2 methods for _Loaded and _Selection changed
i want to select a location in the first combobox and then the next combobox should list a bunch of dates for that specific location
Here is what i have so far:
<ComboBox
x:Name="comboBoxLocation"
Text="Lokation"
HorizontalAlignment="Left" Margin="50,305,0,0"
VerticalAlignment="Top"
Width="120"
Loaded="ComboBoxLocation_Loaded"
SelectionChanged="ComboBoxLocation_SelectionChanged"/>
<ComboBox x:Name="comboBoxDate"
Text="Dato" HorizontalAlignment="Left"
Margin="195,305,0,0"
VerticalAlignment="Top" Width="120"
Loaded="ComboBoxDate_Loaded"
SelectionChanged="ComboBoxDate_SelectionChanged"/>
and
private void ComboBoxLocation_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var comboBoxLocation = sender as ComboBox;
comboBoxDate.SelectedIndex = 0;
comboBoxDate.ItemsSource = controller.GetBusTimes();
//ComboBoxDate_Loaded(sender, e);
}
private void ComboBoxDate_Loaded(object sender, RoutedEventArgs e)
{
List<string> dataDate = controller.GetBusTimes();
var comboBoxDate = sender as ComboBox;
comboBoxDate.ItemsSource = dataDate;
}
This seems to be a lot more difficult than i expected... I am starting to think that i might have madesome fundamental mistake here...
I have been fiddling around with this... I can manage to show a list of locations in the first box and the relevant dates for that location the second box. But when i change the first location, the dates stay the same...
How would i go about this?
There are 2 issues with your code.
It is not clear where & how controller.GetBusTimes() gets information about changed location?
If somehow the above-mentioned function is in know of location change and is still not showing updated info in another combo box then
See also this answer on how to refresh combo box display once ItemSource is changed
Why not to bind separately Item source and selected item?
<Combobox ItemsSource="{Binding ItempsProperty}" SelectedItem="{Binding StrValueProperty, Mode=TwoWay}" />
And you could set one property in the setter of other
Try this, I have used this to link 2 comboboxes in my windows form application.
By default set both comboboxes selection to 0 //Combobox1.SelectedIndex = 0;
private void combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
var combobox1VALUE= combobox1.Text;
}
private void combobox2_DropDown(object sender, EventArgs e)
{
//Select datasouece according to combobox 1 data selection(combobox1VALUE)
combobox2.Items.Clear();
//add data to combobox2
}

get the clicked item from a IsItemClickEnabled ListView in C# and WinRT

how can I get the clicket elevent from a ListView which has the IsItemClickEnabled enabled?
I know how to get the selected Item/Index but not the clicked item.
ItemClick is working but I can not say s.th. like:
Object selection = listView1.SelectedItem;
EDIT:
I have a ListView and I need to catch the clicked item from this list in the following method:
private void listView1_ItemClick(object sender, ItemClickEventArgs e)
{
...
}
I may be missing something, but doesn't the following work for you?
private void lv_ItemClick_1(object sender, ItemClickEventArgs e)
{
var item = e.ClickedItem as String;
}
Here I assume the items in the list are simple strings, but in general they'll be whatever type you are using in the collection you've bound to the ItemsSource property of the ListView.
I guess you can also try the SelectionChanged event, and get the clicked item as e.AddedItems or MyListView.SelectedItem or MyListView.SelectedItems.

Categories

Resources