Resize Window to child content control - wpf

I have a ContentControl that swaps out views based on a 'CurrentPage' property chosen in the ViewModel.
I'm using a Window to host this ContentControl - it contains UserControl views and acts a persistent View/ViewModel for the application.
How do I bind the size of the Window to the size of the currently displayed content?

You don't need to data bind the size of the Window to its content. There is a property called Window.SizeToContent that will have the same effect. From the linked page on MSDN:
Gets or sets a value that indicates whether a window will automatically size itself to fit the size of its content.
A typical example of its use would be this:
// Automatically resize height and width relative to content
this.SizeToContent = SizeToContent.WidthAndHeight;

Related

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.

get designheight/width of usercontrol at run time

Im writing a WPF application where usercontrols are added to a TabControl at runtime - creating a tab for each user control. The problem then is...these controls can have different width and height which means the tabcontrol must adjust its own width and height accordingly. I tought this would be a simple exercise of just accessing the usercontrols Height/ActualHeight properties, but these are NaN/0.0
Is it not possible to get this information?
I can propose next solution:
When you add a new control to the TabControl (is it a TabControl or a TabItem?) set bindings for the Width and Height properties. Create a converter to convert sizes of added controls to the size of Owner (in case if you need to have minimum size).
ActualWidth and ActualHeight properties perhaps are 0 because control wasn't measured yet. Look this thread

Inherit custom class from Panel and VirtualStackPanel

I am implementing a my custom class which needs to be derived from Panel.
After inheriting from Panel, the overridden method "MeasureOverride" gets the default size.
In my xaml page, I am using ListBox for which no width and height is specified and this custom panel is called from ItemsPanel Template.
When the class is derived from Panel class, the width and height received in the parameter is infinity.
But, deriving the same class, from VirtualStackPanel, I get height and width ( the size in which parent control gets displayed).
Any idea, why this behavior? I need to derive the my control from Panel class but in that case needs the parent width & height size (calculated size based on the visible area available).
Regards
Rajesh
Found the solution for same,
Implementing IScrollInfo interface solves the problem. By using IScrollInfo one can implement his/her own scroll containers.

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.

autosize controls in TabPages

I have a TabControl consisting of several TabPages. One of these TabPages contains a TreeView and another contains a DataGridView. I would like these controls (and tabpages) to AutoSize to the maximum size allowed in the TabControl without scrolling.
The DataGridView contains an AutoSize property inherited from Control which garbles the control if enabled. The TreeView does not have this property. I have tried setting the Size equal to TabControl.Size but that does not account for bordersize and the Tabbar height.
treeView.Size = new Size(tabControl.Size.Width - SystemInformation.Border3DSize.Width * 2, tabControl.Size.Height - SystemInformation.Border3DSize.Height * 2);
My question is: how can I determine the height of the Tab buttons or how can I automatically fill TabPages to their maximum size with a single control?
The Property you search is called Dock.
You probably want to set it to DockStyle.Fill on your TreeView and DataGridView.
An alternative would be to set the Anchor Property to AnchorStyle.Top, AnchorStyle.Left, AnchorStyle.Right and AnchorStyle.Bottom (concatenate with binary OR the pipe sign '|').
For that you would need to set the size of the Children to ClientSize

Resources