Listbox doesn't update after adding new items to it - wpf

I have a list box that is bound to an Observable Collection , when I the user add any new items to it it doesn't update itself until the app is closed and opened again.
(I serializes the content of the item source of this list and store it in Isolated Storage)
The only possible solution until now is to set the item source after adding the item in every method that allow the user to add any item but this isn't possible while adding from other pages as I can't access UI elements directly. Any help ?

I solved it ! In the Deserialize method I was pointing to another list (the one that has the result to Deserialization) and that damaged the binding.

Related

Ag grid add new empty rows and create record and send it to database after completing filling information

I am new to react. Is there any way to add a new row to ag-grid on the button click and fill in the details and after finishing filling in the data send the values database. (by not using the forms) just from the table itself?
Thanks in advance
You display data in the grid by binding an array of data objects to the grid's "rows" property.
So, on your "button click", just add a new object to the array
(well, actually, you probably need to clone the array, with the new object added).
Then, when the user edits the empty row, they will be editing the new object that was added to the array.

Opening tab with tree node data

Hi,
I'm new to both Prism and WPF, and I have a question regarding sharing data between views.
Application I'm working on resembles SQL Server Development Studio. Demo contains two regions. First region contains tree (like Object explorer in SQL Server DS). Tree nodes are bound to different view models. Example would be DatabaseA->Tables->dbo.TableA->Columns etc.
Second region is initially empty. When I double click on tree node I would like to open view that displays data I clicked in tree.
In detail:
1. double click on tree node
2. node data and display it in second region
3. if second region isn't empty, check if clicked node data is already displayed in one of existing tab pages
4. if not, create new tab page with clicked node data, otherwise focus existing tab page
Until now, I managed to created tree. When tree node is clicked app calls:
UriQuery uriQuery = new UriQuery {{"ID", unit.Id.ToString()}};
uriQuery.Add("TypeName", "Unit");
var uri = new Uri("DebugTreeItemView" + uriQuery, UriKind.Relative);
RegionManager.RequestNavigate("SECOND_REGION", uri);
This will open view with tab control and I can fetch uri parameters. But I'm not satisfied with this solution. I need a way to:
1. intercept this RegionManager.RequestNavigate call in order to check if tab control is alread created. Also, I need to check that clicked node data isn't already displayed in one of existing tab pages.
2. I would like to send Unit object directly to tab control view instead of sending ID and typename. What is the best way to achieve this?
Thanks.
I would have a ShellViewModel controlling my overall application. It would contain the TreeNode collection for the left side, and the OpenTabs collection for the right side. It would also contain the SelectedTabIndex for the right side, and perhaps the SelectedTreeNode for the left side if it made sense to do so.
When you want to open a new Tab, I would use the EventAggregator to publish an OpenTab event and pass it the selected TreeNode item. The ShellViewModel would subscribe to those events, and would determine if that object already existed in the OpenTabs collection. If it exists, it simply sets the SelectedTabIndex. If not, it adds the item to the OpenTabs collection before setting the SelectedTabIndex
A while back I posted something here on this sort of navigation with MVVM if you're interested

WPF keeping a TreeView list sorted

I have a node on a TreeView that I populate manually and want to keep sorted. Through user interaction the headers on the TreeViewItem's may change and they should move to the appropriate spot in the list.
I iterate through a foreach creating numerous TreeViewItem's and adding them to a parent node. It is all of the children that need to be sorted. I then add a SortDescription as follows.
tviParent.Items.SortDescriptions.Add(new SortDescription("Header", ListSortDirection.Ascending));
This sorts the intial list, but if I change the header for one of the tree view items after it is displayed the item does not sort again. The header text changes, but the items position in the list remains the same.
Is there something I am missing?
I have tried clearning the list and repopulating it, which will work, however it causes some isues in my program as I have a lot of logic for when the selected item is changed and since one of the tree view items in the list that I am clearing is selected it invokes all of this logic when I clear the list and then again when I programmatically reselect the item after rebuilding the list.
The items collection will have to be refreshed upon any change to its property
try the following code after the header edit ....
tviParent.Items.Refresh();
if the code above does not work then try code below after each edit ...
tviParent.Items.SortDescriptions.Clear()
tviParent.Items.SortDescriptions.Add(new SortDescription("Header", ListSortDirection.Ascending));
If you set
yourCollectionView.IsLiveSorting = true;
then it will update automatically.
Also to get this to work recusively put this into a converter like here: https://stackoverflow.com/a/5730402/364429

Dynamic menu items in WPF

Is there a way to create a section on a menu for a list of menu items to be populated by something like an ObservableCollection?
I'd like to replicate the Window functionality in Visual Studio, where the open document tabs are listed in a numbered list, limited to the first 10.
See "Merge ContextMenus" from here. You can find more info by searching for CompositeCollection and menu/ContextMenu.
EDIT: Just saw CompositeCollection was already mentioned in a comment, but I'm going to leave this here for reference.
How does your menu get data right now? Is it databound? Check this article for binding your menu with a collection.
Now it is up to you to add logics when to add item to the collection.
For eg: In your scenario, you have to store the open documents in a list. Then you have to filter out the first 10 documents and add it to the children property of the MenuItem class specified in the article.
You can create menu items in code and manually add them to the menu when the form loads. It isn't elegant, but it gets the job done.

Windows Forms ComboBox problem

I have a combobox that goes out to the database to load it's content on clicking the dropdown arrow. if after the dropdown box is shown with data and I don't select anything, than the current value of the combobox is blown away. Inside my routine to load data, I tried saving the current value and restoring it back after the loading is done. doesn't work.
[Edit] Added the code from the comment here for legibility
MyUltraCombo myultracombo = new MyUltraCombo();
//MyUltraCombo inherits from UltraCombo inside MyUltraCombo, I keep the table name to load from
MyUltraCombo.BeforeDropDown += new System.ComponentModel.CancelEventHandler(cb_BeforeDropDown);
//inside the eventhandler
myultracombo.Load();
//inside the Load method
datatable = DataUtility.GetAllRecords(tablename);
combobox.datasource = datatable;
If you store "the current value" as just the SelectedItem property, then it will probably not be an object that is present in the new list, since it will consist of completely new objects. What you will have to do is store an ID of the current value (if you're lucky to have a unique id) and then search the newly created list for an object with the same id (or whatever you use to uniquely identify an item, maybe just ToString()...).

Resources