Remove item from listbox in XAML - c#

I have a listbox with items in my xaml page. I introduce those items programmatically (I don't know if this is important). I want to delete them as you do in an app like uwp mail.
I want that whenever you click over the left side, a checkbox appears and a button to send to trash those items. Is this a kind of components or on the contrary I have to code it?

If you want to remove the items in a list box, you can do it like this:
listBox1.Items.Clear();
If you want to activate a checkbox in XAML you can add it in the designer, set visible to false. Change it to true in your code behind when you want to activate it and use this to remove a selected item:
listBox1.Items.Remove(item);
You may need to iterate through all the items and check if they are selected.

Related

How to update ListBox control with Dictionary binded to it without jumping to the first item

Is there any way to update a ListBox control with Dictionary binded to it without resetting its DataSource property on every dictionary change? The reason I don't like this solution is that it forces a ListBox control to jump to the first item as mentioned in other questions like this.
Here is a minimal example that reproduces this behavior -- https://bitbucket.org/ntrophimov/updating_issue (about 20 lines of code to read in the MainForm.cs file)
Is there any other solution for this problem?
Is there any dictionary implementation in which I can manipulate items (add and remove them) and these changes will be immediately represented in a ListBox control without refreshing its whole content?
The idea is:
You show your ListBox with items
User click on some item
Applications save the 'selected' item in local variable
User/App initiate ListBox update process
Update process done -> application restore (re-assign) the 'selected' item (if it still valid for current items of ListBox)

WPF ListBox, automatically add "new element" line

In my Control I have a ListBox. Each ListBoxItem is displayed (via ItemTemplate) as a Combobox whose ItemsSource is an ObservableCollection in the ViewModel of my Control.
Next to that ComboBox is a Button to delete an element from the list (the Button's Click Command is bound to a Delete-Command in the ViewModel of my control). This works fine as desired.
Now I want to be able to add new elements to the ListBox. I'd know how to do this by clicking a Button either somewhere outside the ListBox or in the ListBox's ItemTemplate, but instead I would like to add some kind of additional "empty" item as the last item of my ListBox.
This "empty" item should look like any other item, with the exception that the ComboBox has no selection. As soon as the user makes a selection, a new "empty" item has to be displayed. I hope you know what I mean...acutally it's a bit like the "Tags" editor when you post a new question here ;)
Any ideas??? (without breaking MVVM rules)...
Instead of a ListBox, use a DataGrid with CanUserAddRows="True". It will add that "new line" row, and behave exactly as you want.

how to put a button at the bottom of the listbox in wp7?

Windows phone 7.5 and above.
I wanna add a button at the bottom of the list, so the user can click it to fetch more articles.
I put a button inside a listbox in xaml. But it does not work, when the itemsource of the listbox get binded at the runtime, the button disappear.
how to approach the goal?
One solution is to implement that as part of your binding to set the last item to display 'load more...'.
For example, if your listbox template shows a picture, a header and then some description text and you retrieve 25 items each time. What you can do is grab those 25 items from your source then manually add an item with header 'load more...' then bind that list. You can then easily check if the 'load more...' item was clicked by checking the header text or if your model has some unique identifiers for each item then you can give the 'load more...' item an id of -1 or some other unique value.
The other solution is to automatically detect when the users scroll to the end (or near the end) of your list box then automatically load more data. Check these tutorials on how to implement this:
Loading Data when the User Scrolls to the End of a List in Windows Phone 7
Detect when a ListBox scrolls to its end (WP7)
WP7 – How To Extend ListBox When Reaching Last Item

silverlight listbox selection area

Am having a list box item template, using wrap panel in container to show as three column listbox. working well. now the issue is, i have to capture the selection change only when user clicks on the second column.
is it possible to set the selection area in listbox?
I don't think it is possible to somehow raise selection change event only when the second or third column is selected. One thing you can do is keep last selected column index in a variable and inside your selection change event judge whether the new selected item belongs to same column or not if it doesn't simply ignore the change
I agree with Haris, I don't think it it possible. You should be able to use the mouse down event and then in code figure out the selection index from that. You could possibly use LINQ on the list box's ItemSource and find the match or use the Tag property of the item.

WPF: DataBinding a ListBox where each item is a Tab Stop

I just built a WPF form that contains a ListBox. The ListBox has bound to it a list of TextBox controls. I need to make each TextBox control a TabStop so that a user can hit tab, type in a number, hit tab again and type in the next number, etc.
Problem is, the ListBox itself catches the tab then the next tab skips to following control after the ListBox.
Is there a way to make each TextBox inside the ListBox be tabbable (or perhaps another type of databound control that would work)?
Thanks
Well we don't really have enough information to answer the question (this depends on what Templates and Style the ListBox is using) but you'll potentially need to play with the KeyboardNavigation.TabNavigation property to change how to cycle through the items and set IsTabStop on the ListBox to false.
Something like:
<ListBox DataSource={Binding} IsTabStop="False" KeyboardNavigation.TabNavigation="Cycle" />

Categories