How to create a custom control that can be larger than its generated size using WPF

I have created a custom control using WPF and I want to add it to the window. I did this, but I cannot control my level higher than the height than its own xaml file. My MaxWidth and MaxHeight are infinite, but I can't seem to make the control taller than the one in its xaml file.

To get around this, I have to make all my user control huge, so I can sort it as I wish. It doesn't seem right, I must be missing something.

+1


source to share


6 answers


Removing height and width is the way to go. The designer (blend) has special designer width and height properties that they can use for design, but will not set the height for runtime.

xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 mc:Ignorable="d" d:DesignWidth="412" d:DesignHeight="230"

      



This is the xaml that will be at the top of the / UserControl window. This should help explain things.

+2


source


Why would you want your control to be higher than its height in its own XAML file? Could you just remove the height in the XAML control file and explicitly set the height of the control when you declare it in other XAML files (or code) that use it?



0


source


If I remove the height and width in the XAML file of the controls, I lose the ability to use the constructor for my custom control. Short answer that solved my problem, but now I cannot use the constructor for the custom controls. It doesn't look like I'm better.

0


source


The problem might be that the inner controls in your custom control are not stretching to your control. Try setting HorizontalAlignment = "Stretch" or Width = "Auto" on the inner controls, or you can try binding the Width property.

0


source


Ok, after some further investigation, I was wrong. its a lattice which is causing the problem. If I set the Grid Width and Heigth to Auto then everything works fine, but I lose the ability to use the constructor.

I have all the alignments set to Stretch for both the Grid and its controls.

So, everything works fine if I set Grid.Width = Auto and Grid.Height = Auto, but when I do, I lose the ability to use the constructor.

0


source


I don't know of any width / height attributes for the VS constructor if that's what you are using. I have used the MinWidth / MinHeight attributes in my xaml quite efficiently, however, to handle the situation I think you are describing.

0


source







All Articles