Home > Net >  Combining commonly used WPF-Controls for Reuse
Combining commonly used WPF-Controls for Reuse

Time:05-31

I keep repeating the same code over and over:

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
    <StackPanel Orientation="Vertical">
        <Label Content="Content 1" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <ComboBox Style="{StaticResource CustomComboBox}"/>
    </StackPanel>
</StackPanel>

It is always some combination of an Input-Control (TextBox, ComboBox, ...) and a Label.

I have scouted

CodePudding user response:

A ContentControl should use a ContentTemplate:

<Window.Resources>

    <DataTemplate x:Key="ComboWithHeader">
        <StackPanel Orientation="Vertical">
            <Label Width="120" Height="25" Content="{TemplateBinding Content}"/>
            <ComboBox Width="120" Height="25"/>
        </StackPanel>
    </DataTemplate>

</Window.Resources>

<Grid>
    <ContentControl Content="My Header" ContentTemplate="{StaticResource ComboWithHeader}"/>
</Grid>
  • Related