Is there any way to make two controls that are in different containers the same size in WPF? For example, suppose you have two textboxes: textbox1 and textbox2. Textbox1 is in a grid and its size can grow and shrink when the user resizes the window. Textbox2 is in another part of the window and I need it to always have the same size as textbox1. Is there any way to do this?
Keep in mind SharedSizeGroup will not work because the textboxes are in different containers. Also, I've tried binding textbox2's height property to textbox1 and that doesn't seem to work either. Finally, I tried catching textbox1's SizeChanged event, but its Height property is always NaN for some reason.
You should bind to ActualWidth and ActualHeight members of the TextBox.
Related
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
I have a wpf cusotm control derived from combobox System.Windows.Controls.ComboBox the control template is redefined and the popup (PART_Popup) contains a DataGrid and some other control. I use it in two places - in TabControl(,Grid) and in a Window(Grid,,Grid). When the DataGrid is resized either by dragging columns or from code the behavior is different: in window the Popup resizes in TabControl Popup remains the same and a scrollbar appears if necessary.
I need to know what can cause such difference.
Edit
Datagrid is has HorizontalAlignment="Stretch"
I find it. By mistake there was
Popup.Width=Me.Width
instead of
Popup.Width=Me.ActualWidth
This caused resizability in case when the Me.Width was not set.
I have ContentControls like ComboBox and TextBox inside of a Grid or a StackPanel. I want them to have the maximum possible width. How do I achieve that? I've noticed that Button has maximum width any way when it is added to a StackPanel, but ComboBox and TextBox do not. I've tried setting ContentControls' Width="*", but that doesn't work.
Did you try HorizontalAlignment="Stretch"?
I have a ListBox that contains a number of User items that are DataTemplated to appear as UserControls in the ListBox. Each UserControl can be expanded in size. To start with, the ListBox is big enough to display them all in their unexpanded state. The problem that I have is that when a number of these UserControls are expanded together, they extend out of the ListBox's visible area. The ListBox does not recognise this fact and no ScrollBars appear, even when they are set to Visible.
I am using DoubleAnimations to alter the UserControl heights when the user clicks on a button in each one. Is there something that I have to do, or some setting on the ListBox that must be set to get it to register the size changes of the UserControls that represent its items and display ScrollBars when needed?
Edit>>>
I have tracked down the problem to a custom WrapPanel that I am using in the ListBox.ItemsPanel. When I remove it, or replace it with a standard WrapPanel, ScrollBars appear when required. I got the code for the Panel from a good article about creating custom WPF panels. Can anyone see what's missing from the code given in the article and why it might stop the ScrollBars from displaying?
I wonder whether ListBoxes normally do what you are expecting? You might try calling InvalidateMeasure/Layout on the ListBox if you know when the item sizes change, just to see?
I decided to write the custom WrapPanel code again completely and this time it worked correctly! When comparing the new version with the previous version, I could see that a + was missing from a += in a measuring calculation and so the Panel thought that the items were much smaller than they really were... hence no ScrollBars.
So, if you have this problem, check your measuring code carefully.
I have a WPF UserControl (Foo.xaml) that should be displayed streched (VerticalAlignment="Stretch" HorizontalAlignment="Stretch") on a window.
If I don't specify Width/Height on the UserControl it will be displayed as intended at runtime. Unfortunately the VS WPF designer will not show the control correctly if I open Foo.xaml (size may be 0x0 depending on the child controls).
If I specify a size on the UserControl the designer will work correctly with Foo.xaml but then it won't strech at runtime.
How can I fix this so that it works at design and runtime?
It sounds like your UserControl is only useful if it is a certain minimum size. If so, set the MinWidth and MinHeight properties.
You can set the DesignWidth and DesignHeight properties on the UserControl to adjust the height and width of the control in the design view.
You can adjust those properties with the mouse by dragging the square rectangles that show up when you select the resize tool for the UserControl.