wpf "Half the Whitespace" Design principle How to use this in real code

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

To generalise what we've demonstrated above: individual things contain a fixed margin of "half-the-whitespace", and the container they are held in should have a padding of "half-the-whitespace". You can apply these styles in your application resource dictionary, and then you won't even need to mention them on the individual items. Here's how you could define "HalfTheWhiteSpace":

<system:Double x:Key="DefaultMarginSize">2</system:Double>
<Thickness x:Key="HalfTheWhiteSpace" Left="{StaticResource DefaultMarginSize}" Top="{StaticResource DefaultMarginSize}" Right="{StaticResource DefaultMarginSize}" Bottom="{StaticResource DefaultMarginSize}"/>

Then I can define a base style to base my other controls styles on: (this could also contain your default FontFamily, FontSize, etc, etc)

<Style x:Key="BaseStyle" TargetType="{x:Type Control}">
    <Setter Property="Margin" Value="{StaticResource HalfTheWhiteSpace}"/>
</Style>

Then I can define my default styling for TextBox to use this margin:

<Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}"/>

I can do this kind of thing for DatePickers, Labels, etc, etc. (anything which might be held within a container). Beware of styling TextBlock like this... that control is used internally by a lot of controls. I'd suggest you create your own control which simply derives from TextBlock. You can style your TextBlock to use the default margin; and you should use your TextBlock whenever you explicitly use a TextBlock in your XAML.

You can use a similar approach to apply the padding to common containers (e.g. ScrollViewer, Border, etc).

Once you've done this, most of your controls will not need margins and padding - and you will only need to specify values in places where you intentionally want to deviate from this design principle.



Got any wpf Question?