ContentPresenter 不以样式显示自定义控件

ContentPresenter not displaying Custom Control in Style

我有一个自定义控件,NumericUpDown。我创建了一个命名样式,我想在其中扩展控件,在本例中是通过将它包装在一个网格中并添加一个标签。

当我使用按钮作为基本示例时,我可以很好地扩展 ControlTemplate。原始按钮显示在 ContentPresenter 的位置,我可以在其周围添加其他控件用于布局等。

ControlTemplate TargetType="{x:Type Button}">
    <Border BorderThickness="3" BorderBrush="Red" Background="{TemplateBinding Background}">
        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
    </Border>
</ControlTemplate>

但是,当使用我的自定义控件时,ContentPresenter 不显示 NumericUpDown 控件,它完全不显示任何内容。即使是最基本的形式,我也无法让 ContentPresenter 显示自定义控件。

<Style x:Key="LabeledNumericUpDown2"  TargetType="{x:Type controls:NumericUpDown}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type controls:NumericUpDown}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="5*"/>
                        <ColumnDefinition Width="5*"/>
                    </Grid.ColumnDefinitions>

                    <Label Grid.Column="0" Content="Blabla"/>
                    <ContentPresenter Grid.Column="1"/>
                    <!--<controls:NumericUpDown Grid.Column="1"/>-->
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我这辈子都弄不明白我错过了什么。

感谢您的帮助,非常感谢!

编辑

NumericUpDown 自定义控件还具有定义 NumericUpDown 控件的默认样式。我希望用命名样式对此进行扩展。

<Style TargetType="{x:Type controls:NumericUpDown}">
    <Setter Property="HorizontalAlignment" Value="Stretch"/>
    <Setter Property="VerticalAlignment" Value="Top" />
    <Setter Property="Margin" Value="0" />
    <Setter Property="Height" Value="18" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type controls:NumericUpDown}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="16" />
                    </Grid.ColumnDefinitions>

                    <TextBox x:Name="PART_TextBox" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Top" Padding="0" Margin="0" 
                        Text="{Binding DisplayValue, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" />

                    <Border Grid.Column="1" BorderThickness="0 1 1 1" BorderBrush="#FFABADB3">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="8" />
                                <RowDefinition Height="8" />
                            </Grid.RowDefinitions>

                            <Button x:Name="PART_ButtonUp" Grid.Row="0" Content="▲" FontSize="4" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" BorderThickness="0" Padding="6 1 0 0" />
                            <Button x:Name="PART_ButtonDown" Grid.Row="1" Content="▼" FontSize="4" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" BorderThickness="0" Padding="6 1 0 0" />

                        </Grid>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

一些 Controls 定义并期望特定的命名元素能够正常运行,否则称为部件。

请确保您的 ControlTemplate 已提供 Control 所需的部分,以便其正确呈现。

编辑:

试试这个 ControlTemplate 让你开始。

<ControlTemplate TargetType="{x:Type controls:NumericUpDown}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="5*"/>
            <ColumnDefinition Width="5*"/>
        </Grid.ColumnDefinitions>

        <Label Grid.Column="0" Content="Blabla"/>

        <Grid Grid.Column="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="16" />
            </Grid.ColumnDefinitions>

            <TextBox x:Name="PART_TextBox" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Top" Padding="0" Margin="0" 
                Text="{Binding DisplayValue, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" />

            <Border Grid.Column="1" BorderThickness="0 1 1 1" BorderBrush="#FFABADB3">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="8" />
                        <RowDefinition Height="8" />
                    </Grid.RowDefinitions>

                    <Button x:Name="PART_ButtonUp" Grid.Row="0" Content="▲" FontSize="4" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" BorderThickness="0" Padding="6 1 0 0" />
                    <Button x:Name="PART_ButtonDown" Grid.Row="1" Content="▼" FontSize="4" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" BorderThickness="0" Padding="6 1 0 0" />

                </Grid>
            </Border>
        </Grid>
    </Grid>
</ControlTemplate>