Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF control default size

When defining custom resource themes for a wpf application, I can set width / height etc... How can I find the default value for these properties (i.e. the values used in the controls provided in the framework) ?

like image 793
fabien Avatar asked Jul 21 '11 08:07

fabien


1 Answers

WPF controls don't generally contain any kind of default size. One of the main functional points of WPF is that everything resizes dynamically unless you specify a size.

If you do want to measure the amount of space a control would like to have if given infinite space, you can create it, call Measure on it with a Size of +infinity, +infinity, and then check DesiredSize. For most controls, this will give you the minimum size the control wants. If you give a fixed size in Measure, some controls will return that they want all that space as they size to their container (e.g., Grid, TextBox, Button...). Some controls size only to their content, so they'll tell you they want no space (e.g., StackPanel).

So you have to ask yourself why you would even think of the concept of a default size in WPF when almost all controls are made so that they either size to their content or size to their container depending on how they are set up. The main thing you'd want to measure is text, and you can do that with the trick above for items like TextBlock or images.

Edit: to query any DependencyProperty for a default value, use the property's metadata:

double defaultWidth = double.NaN;
PropertyMetadata widthMeta = TextBlock.WidthProperty.DefaultMetadata;
if (widthMeta != null && widthMeta.DefaultValue is double)
    defaultWidth = widthDefault.DefaultValue;

To Reset a DependencyProperty to its default value, call DependencyObject's ClearValue:

MyTextBlock.ClearValue(TextBlock.WidthProperty);

To check for a locally set value:

if (MyTextBlock.ReadLocalValue(TtextBlock.WidthProperty) != DependencyProperty.UnsetValue)
like image 68
Ed Bayiates Avatar answered Nov 15 '22 06:11

Ed Bayiates