Many WPF control template definitions do not take into account the Member is null (default value), such as the headeredcontentcontrol control template defined below:
<ControltemplateTargettype="Headeredcontentcontrol">
<Dockpanel>
<BorderDockpanel. Dock="TOP"
Borderbrush="Red"Borderthickness="2"
Padding="5">
<ContentpresenterContentsource="Header"/>
</Border>
<Contentpresenter/>
</Dockpanel>
</Controltemplate>
Then headeredcontentcontrol is defined as follows:
<HeaderedcontentcontrolHeader="ABC">
<Button>Def</Button>
</Headeredcontentcontrol>
Not bad. If you omit the header attribute (the default value is null), the result will be:
The header is null at this time, while the border is still there, and the padding definition also occupies a certain amount of space.
Solution: When the controltemplate is defined, when the attribute is null, add a trigger to hide the corresponding control.
Therefore, the above controltemplate should set a name for border, and then judge whether the header is null through trigger. If it is null, change the visibility of border to collapsed. Of course, the content attribute does not need to be determined to be null, because the content attribute corresponds to the contentpresenter without any other station controls, and the contentpresenter does not occupy any controls (if the content is null ):
<ControltemplateTargettype="Headeredcontentcontrol">
<Dockpanel>
<BorderDockpanel. Dock="TOP"
Name="Border"
Borderbrush="Red"Borderthickness="2"
Padding="5">
<ContentpresenterContentsource="Header"/>
</Border>
<Contentpresenter/>
</Dockpanel>
<Controltemplate. triggers>
trigger property = " Header " value =" { x : null } " >
<SetterTargetname="Border"Property="Visibility"Value="Collapsed"/>
</Trigger>
</Controltemplate. triggers>
</Controltemplate>
In this case, the display is correct: