WPF ListBox, automatically add "new element" line - c#

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.

Related

Remove item from listbox in XAML

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.

Update DataTemplate for a specific item on a listbox on Windows Phone 8 C#

I'm not being able to update a single item's DataTemplate on a list during runtime. In detail, here is what I'm trying to accomplish.
I have a Listbox where the items can have different states (Collapsed, expanded, disabled, etc), each with a different layout. I'm using a TemplateSelector to choose the correct DataTemplate according to a property on my class and that's working all great when I first create the list, the items are shown properly. However, when I change the property that sets the DataTemplate in runtime, the NotifyPropertyChanged is called and the information of the item is updated on the list, but not the DataTemplate. For example: I have a collapsed item with a label X that i want to expand. I click on the item and the label changes to Y but the DataTemplate doesn't update.
Any idea on how I can do this? Can't the DataTemplate be updated during runtime unless it is for the whole list?
I'll appreciate any help.
Make UserControl and use it inside your data template. Now, to change the state you can call methods on this UserControl and it will update. You can use animations via storyboard too.

Accommodate custom item on winrt listview

I'm developing windows metro app. In my application, I've one Listview with wrapgrid in itemspanel to display list of items on vertical rows with specific height. I want to display one item on top of the first column of list view, which shows result/stats of list items.
I would like to know if it is possible without adding custom item to datasource of listview?
ListView has a Header property in which you can Place content before the ListViews Items.
As commented, I've implemented following solution which is not elegant but worked for me.
Use Datasource converter to add dummy item in main list. So, my original list remains as is.
Use Template Selector to bind different template for first item.
Handle Selection and clicked event of dummy item.

Winform ListView databind

I have a Listview that uses databind. I set the DataSource property to a binding source. All works fine. The problem is that I need to have a column that is not databinded and contains only buttons that have the same handler for click event. To accomplish this I tried to add a subitem that is a button for each ListViewItem after InitializeComponent but doesn't work, nothing is displayed. Also I set the list view column type to Control.
If I add elements to ListView and isn't databinded that the buttons appear.
So it will be a great help for me to know if buttons could be displayed in column that is not databinded when the listview uses databinding for rest of columns.
Thanks!
The best thing to do here (assuming you mean ListBox), is to have a single button above or below the listbox, that uses the ListBox.SelectedItem property to investigate the selected item and do something with it.

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