Databinding to a different window - wpf

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.

Related

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?

Pass a value from child to parent in Silverlight

I am working on Silverlight project.
I added a custom user control (Say, control1) which has a text box and button to a xaml page (Say, Page1).
Now what I want to do is when users clicks on the button, i want to pass the value in the textbox to Page1 and do something.
So basically, I am looking for a way to pass back a value from child to parent page in Silverlight.
Thank you.
You should look into the Model View ViewModel (MVVM) pattern. It works very well with WPF and Silverlight.
http://en.wikipedia.org/wiki/Model_View_ViewModel
http://karlshifflett.wordpress.com/mvvm/ (lots of good information and demos)
You can do this through binding. Bind the Text value of the TextBox to a string property in your ViewModel and use that property throughout the code.
All the controls within your user control are accessible within your main page. If possible, write the click event of the button within the main page and you'll be able to access any control's property. Hope that work for you.

WPF element binding across separate windows

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.

Hook in datatemplate view switch/change

I am switching my UserControls via DataTemplate. When I leave the UserControl/DataTemplate View I need to ask wether the user wants to save or not because else all data will be lost like graphical location points x,y of a user drag/dropped Rectangle on a canvas.
How can I hook into the datatemplate change and bring up my own save dialog?
So you're binding a ContentControl's Content property to a property in your viewmodel?
The best place to implement this workflow would be where you normally set this viewmodel's property. Doing this in the viewmodel, rather than trying to hook into events in the View layer means you have more control and testability.

Wpf custom control

I have a customn control which display a window with another custom control inside. The developper can redefine the control template, it works but I don't find a way to allow developper to redefine the template of control inside the window that the custom control display...
Ok, I found. The solution is simply to set the other control as a dependency property inside the first (parent) control...

Resources