So I have a listbox and a class. This class is called 'Mission' and has three properties: Name, IsStarted and IsCompleted.
I have used a BindingList and set the datasource of the Listbox to that BindingList. This works fine, no problems so far.
What I don't know how to do (and I hope it's possible) is to bind the properties IsStarted and IsCompleted to two CheckBoxes, so that if I change which Mission is selected, those CheckBoxes will change to reflect the properties of the SelectedItem.
I appreciate I can do this by using the event of SelectedItemChanged on the ListBox, but I would like to have a two way binding so that if I were to check or uncheck the CheckBoxes, the data in the class would change.
You don't need to use events, it's enough to use data binding:
missionsListBox.DataSource = bindingList;
missionsListBox.DisplayMember = "Name";
isStartedCheckBox.DataBindings.Add("Checked", bindingList, "IsStarted");
isCompletedCheckBox.DataBindings.Add("Checked", bindingList, "IsCompleted")
This way, the ListBox will act as an index for mission objects and it shows a list of missions. When you select an item, it shows values of IsStarted and IsCompleted in corresponding check box controls and you can change those values for selected item using check boxes.
Related
I encountered a very specific behave on the Comboboxes in Winform. I found a lot of quite similar questions, but no useful anwser to my problem:
I have a form where controls are bound to a SQL datasource via TableAdapter/manager. Some of the values are linked to Lookuptables which are of type Combobox. When I first fill the base dataset, all is shown correctly. I can choose items from Combobox (fields id and NamedItem) where "id" is bound as datamember to the base datset table and "NamedItem" is the DisplayMember of the Combobox.
No problems when changing values or updating but ...
once I have to refill the Combobox underlaying datasource (eg. in case, an entry was added), the selected Index of the Combobox is set to 0 - means, the first item in the list.
Sure, I could remember the selectedindex and restore it after. For one Combobox, no problem, but for dozens ...
Is there any other way to keep the last selecteditem after refilling the combobox?
If items would not be deleted from your combo list , turning the Clearbeforfill property of your Table Adapter to False can prevent that.
I have a DataGrid with user sortable columns. ItemSorce is bound to a CollectionViewSource that applies a default sort. The CollectionViewSource is in turn bound to an ObservableCollection. I want the first item to be selected when the screen first loads.
setting SelectedIndex doesn't work because I have SelectedItem bound to a property that is initially null, and I need that binding to be two way.
I can't set the property bound to SelectedItem to the first item because the collection is sorted on the view side and I don't know what the first item is.
Help please.
I'm not sure this will work for you, but you can try:
Give your ViewModel a name (if you have one) so you can access it by its name;
Register an event handler to the Loaded event of your DataGrid;
In the handler get the first item of the DataGrid through var firstItem = YourDataGrid.Items.FirstOrDefault() as YourModel;
Set the SelectedItem of the ViewModel to the item we've got in point 3. 'YourViewModel.SelectedItem = firstItem;'. If the SelectedItem notify the ui when it changes, aka is observable, your UI should update.
I am new in WPF, and I try to implement a dialog view with one Combobox (ComboboxView) that loads Enumerations and Lists. I show data in a Datagrid and when I click on a cell depending on the type of the cell (an Enum cell or a 'list' cell), I call this ComboboxView and I load the Enum items or the whole list and I set the current selected value. The property ItemsSource of the combobox has a binding with ViewModel property Sources. It can be a list of EnumItem or List
How can I implement one view that loads Enums and List in the same way?
Thanks
I have a method that I call in a form that returns a List and then I throw that into a BindingSource and throw the BindingSource into a DataGridView. The Order class has a property called Items that contains a List. My Order and Item class both implement the INotifyPropertyChanged Interface.
What I'm trying to do is that when a row is clicked in the DataGridView it pulls up the list of items associated with the order from it's Items property into another grid. I want to ensure that those items are 2 way binded to the grid but I can't call it the way I'd like to...
itemsGrid.DataSource = orderBindingSource[orderGrid.CurrentRow.Index].Items;
I can't access the properties of the list from within the BindingSource (After the . operator, Items is not an available option)... Any recommendations? I've tried using a BindingList<Order> and BindingList<List<Order>> but was getting implicit conversion errors. Any suggestions?
*EDIT: Ok I've managed to get the BindingList to work. I didn't know I had to add the list like this
orderList = new BindingList<Order>(Method To Retrieve List<Order>)
Aside from this, the first grid displaying the orders works and updates perfectly. The sub grid which contains the Order's Items bound like
itemsGrid.Datasource = orderList[ordersGrid.CurrentRow.Index].Items;
It displays properly. I don't have editing enabled as I have a few textboxes that are setup and each textbox is bound to the Item from the orderList of the currently selected row of the ordersGrid. I can update the data in the textboxes for the selected item in the subgrid but the changes when I leave the textbox, the sub grid's row for that item property edited does not update as I leave the textbox. It will, however, update if I select another item in the grid. How can I get it to update as I leave the textbox instead of having to click on another row in the grid?
I have a Combobox, in which I would like its items to be the column data that is located on a DataGrid. Is there anyway to set the Combobox itemsource to be a specific column of a DataGrid?
Right now I'm iterating each row of the DataGrid, getting the field's data and adding them to the Combobox, but that means that I would have to clear all the items and reiterate everytime the DataGrid is modified.
You can set ItemsSource and DisplayMemberPath properties:
comboBox1.ItemsSource = dataGrid1.ItemsSource;
comboBox1.DisplayMemberPath = "ColumnName";
I Think you'are taking the wrong approach. Your data grid must be bound to a collection of object. I guess you could just build another collection by extracting the desired fields (for example with linQ) and expose this new collection to your view such that you can bind your combobox.
I you want to keep this second collection updated, make your first main collection an ObservableCollection such taht your can subscribe to CollectionChanged Event. In the event handler, just manage the add and remove in your combobox source collection.