Update combobox in window 1 after updating window2 Vaadin - combobox

I have a Vaadin window1 which contains a ComboBox of items from database. When I select an item of this ComboBox I have a window2 which appears. This window2 gives me the possibility to add data in database. Now I want to see the new item in my ComboBox in window1 after closing window2. Have you any idea please how to achieve that?

You need to refresh the container data source. In my case, I had a JPAContainer that has a refresh() method, see documentation. If you use SQLContainer then have a look at its documentation.
If you use no special container but the ComboBox method addItem, then you should call removeAllItems and add the new items (by performing the DB query again).

Related

Access Datagrid from ViewModel

I have a simple WPF window with a Datagrid and an "Add" button. The add button is wired to the ViewModel using a RelayCommand, and basically just adds a new object to the observable collection and then sets the selected item (also databound from the grid's SelectedItem property). The end result is a new row is added to the grid and is then set to the selected row. This is almost the result I'm trying to achieve, because the next step is to put the grid in edit mode by invoking BeginEdit. The problem is, from my understanding, the VM isn't supposed to access controls on the view directly.
So my question is, what would be the most "correct" way of achieving this using MVVM?

Adding the userControl to the DockPanel didn't effect Dynamically in Wpf

I am new to Wpf. One UserControl (Wpf UserControl) is added to
the DockPanel and in some action O replaced the first UserControl
with another UserControl in DockPanel dynamically. But the first
UserContorl remains visible and the second UserControl in not showing
in DockPanel.
I don't know the correct behavior of how I can be
loaded dynamically. I added this code (this code is executed well)
but it doesn't reflect the action to display "CtlAddEmployee"
control in Dockpanel.
CtlAddEmployee frm2 = new CtlAddEmployee(str);
DockPanelInRibbon.Children.Clear();
DockPanelInRibbon.Children.Add(frm2);
For full details how it is working is asked in my previous question
in detailed way:
Loading another userControl in wpf Ribbon Window when the we click on DataGrid row
Please provide the solution for this.
yaa here i need to refer old object but not the new created object for the corresponding class reference...
For that better to use parameterized constructors..

WPF multiple windows, change combobox affects both bindings

I have a listbox which lists user details and on selection opens that user detail in a new instance of the user detail window
using UserDetailWin udw = new UserDetailWindow();
udw.show();
Comboboxes in the window are populated by a shared CollectionViewSource and SelectedValue is done through TwoWay binding.
The problem is when I have more than one UserDetailWin open and change the combobox selection in one all the others get that selected id.
Anyone know what I'm doing wrong?
Thanks in advance
Kier
This is normal behaviour. If you understand object references, then it should be easy to figure out by yourself.
To fix this, you should create new instance of CollectionViewSource for each combo box.
You are using same DataContext for all opened windows. Make selected user DataContext of new window and bind all variables directly (by setting DataContext property of UserDetailWin).

Dynamically Load UserControl using MVVM Light Toolkit

I searched this site and i found 2 Links
how to load wpf usercontrol in MVVM pattern
MVVM-Light: Load UserControl into Window
but still i cant find the answer to my problem and this link
MVVM-Light Locator Pattern and Reusable UserControl
i Didn't Understood.... so here is i am stating my problem which might help others struggling like me ......
I have a MainWindow which has 2 parts one has a TreeView(a
UserControl) and the other Displays different user controls(named DisplayPanel).... just like windows Explorer.
The Display Panel on the Right side will display different user controls on Clicking nodes of tree view.
and my TreeView is Itself a user Control.
How can i make this composite UI Work using MVVM. Also I am planning to use MVVM light Toolkit. Does this have something that can help...
An Example will be great
Thanks... :)
Edit
My TreeView in a UserControl I made a dependency property in the UserControl which catches the selected Item fo the tree view so that i can use this dependency property to populate the required view in the "MainView" ContentControl binding....as you advised me in the comments. Everything is till now
Problem is that i want to display data contained in the the selected item and i cannot set the DataContext of the UserControls(which will be displayed in right hand side) to the selected item as then i will not be able to use my view model for the respective usercontrol for commands and other operations
I tried to solve this too.... i used the Mediator (Messenger) in my TreeViewUserControl view model to send a Message to the Usercontrol's(the one that i need to display) view model . Message will be passed whenever the item is selected in the tree view. and message contains the selected node. I forgot to mention i set the datacontext of the UserControl to its view model so that i could display data
But using this approach the problem is that when the I click a type of node for the first time the data is not populated but if the same type of node is clicked again its populated. What’s happening is that UserControls object is availabe when the the tree item is clicked for the first time and Mediator sends the message. So Mediator is not able to pass the message to the userControl view model.....
I totally do not have ne idea to solve this further.... is my way if displaying user control right or I should do something that else....totally confused.....
You could try defining a DataTemplate for each type in the TreeView's ItemsSource and instead of having a specific UserControl on the right side, just bind to the TreeView's SelectedItem. Alternatively, you could use a DataTemplateSelector.
Edited for OP's Edit
Did you do this?
MainWindow has TreeView whose ItemsSource=Binding MainVM.Items.
MainWindow has ContentControl whose Content=Binding TreeView.SelectedItem.
Somewhere in project, have ResourceDictionary where each possible type in MainVM.Items has a DataTemplate defined?
Which ViewModel (MainVM or ItemVM) are you trying to use and why can't you use it?

WPF - call a window function from a child page

I have WPF window that uses a dockpanel and the menu control. I have the code to create the menu options based on a user ID.
Within this window, I have a frame that contains a WPF page. I carry out all the authentication on the page and then have a user ID for the window to use. However, I cannot get the parent window to "refresh" and create the menu bar with the new ID information. When the window loads, I do not run through the commands to display the menu bar. I have tried putting that in its own, public, function and calling it from the page but that does not seem to work.
There must be a window method that I'm missing that can make the menu bar display based on the call from the page.
It sounds like you're still thinking in procedurally, in WinForms style. What you describe would be necessary in WinForms, but in WPF it is usually much easier: just use data binding. As long as your menu items are generated from a "UserID" dependency property (or enabled/disabled based on it), then all you need to do is set the UserID DependencyProperty and the UI will update itself with no additional code.
Here is how to get the UserID into a DependencyProperty of the Window or a context object:
In your Window or a context object create a "UserID" DP
Make your Window or your a context object the DataContext of the page
At the end of the authentication code, set DataContext.UserID (or alternatively create a UserID property on the page, and have the Window bind to it with a two way binding)
Once you have the UserID in a DependencyProperty, there are many ways to update the menu items automatically whenever it changes:
In each menu item, bind its visibility to the UserID DP on the window using an appropriate converter (using the converter parameter to distinguish between items), -OR-
Use a converter for setting ItemsSource so you filter your items, -OR-
Create a PropertyChangedCallback that sets a filter on the CollectionView you use for menu items, -OR-
Some other technique (there are many other good ways to do this)
For typical situations we're talking about less than 10 lines of C# here, not counting the DependencyProperty declarations.

Resources