WPF element binding across separate windows - wpf

I can do element-to-element binding in WPF: For example, I've got a window that has a slider control and a textbox, and the textbox dynamically displays the Value property of the slider as the user moves the slider.
But how do i do this across separate windows (in the same project, same namespace)?
The reason is that my main application window containing the textbox has a menu option that will open an 'options' window containing the slider control.

You should use a (global) ViewModel, containing the data you need to share, and bind to the property from that ViewModel.
This way the changes in either of windows are reflected in the bound data object, and back.

You dont. Point. Databinding has to go to an element accessible in the same control.
What you can do is have the options menu bind go an object that it has in it's own code (property) that gets populated to the same object the othe rwindow uses as data source.

Related

Call method in view when a property changes

I work to an application using MVVM pattern. In view i have a text box and a canvas. In canvas I will draw some lines, as many as the text box indicates. The text of text box is bind to a int property in view model. Also the text box has a command that updates the property in view model. I know how to rise property changed event on view model.
My question is: Is there a way to call the drawing method from view when property changed event is raised?
How about putting the canvas/drawing code in a separate user control with the number of lines as a dependency property bound to the ViewModel. You can then handle the dependency property change event inside the user control and do the drawing.

Databinding to a different window

In my WPF application I have a main windown with a canvas and a few sliders.
Inside this canvas there are multiple versions of a custom UserControl. The user control is very basic, just and image and a textblock. I want to bind the text size of the textblock inside the user controls to one of my sliders in the main window.
I have no idea how to point the binding to a different window!
Do you use a view model? If yes, you can create a property in the view model with change notification and bind it to the parent windows slider and the same to the user controllers text block properties.
create a dependency property in your usercontrol and bind your slider value to it.

WPF textbox not losing focus when selecting custom control

In a WPF/MVVM application I have a custom control on a particular view. This control extends WPF DataGrid and contains User names and ids.
On the same view I have some textboxes whose Text properties are bound to all different properties of User object exposed by the viewmodel and UpdateSourceTrigger for the Text properties are set to LostFocus.
Data gets updated as they should be whenever I leave a textbox (since the textbox looses focus). But problem is, this doesn't occur when I select any item in the custom control leaving any textbox, textbox data doesn't update. Can anyone explain what's happening?
Perhaps your custom control has its own focus scope defined, thus allowing logical focus to be in both the text box and your custom control? Try checking in snoop.

Expand Usercontrol to full window size

I am currently working on a project which has a tab control which contains a Wrap panel which contain a series of user controls. I am looking for a way to allow the user to select one user control and maximize it to the size of the tab control/window.
One thought is to simply remove all the other items from the panel.However I am attempting to use MVVM as much as possible and I'm not sure how much the user control should know about the panel. (The user control will contain a button to allow maximizing)
Is there a way to temporarily remove the usercontrol from the grid and treat it like a modal popup or just to fill the window?
How about having "Visible" or "Maximized" bool properties in the view model for each user control based item, and databind said user controls Visibility property to the appropriate property. Then bind your user controls maximize/restore button to command in the view model to change the VM properties appropriately?

In a WPF tabControl how can I dynamically select a tab page from an object's type?

I've implemented the MVVM pattern and have some viewModels that are bound to tab pages on a tab control.
When a specific object type changes (i.e. from Car myVehical, to Bike myVehical), then i want the relevant tab page to become selected.
Thanks.
You could hack it by using a custom IValueConverter. You could then bind TabControl.SelectedIndex to a property on your view model and use the converter to convert from the type (Car or Bike) to an index. The value converter code would have to be updated when you change the tabs.
Rather than using a tab control, you can use DataTemplates. The view itself will get set automatically based on the object you set as the content.

Resources