2

ボタンのリソース スタイル コントロール テンプレートがあるので、さらにボタン自体にコントロール テンプレートを定義しましたが、ボタンのコンテンツは表示されませんでした。どうすればこれを解決できますか?

<Grid>
        <Grid.Resources>
        <Style TargetType="Button">
            <!--Set to true to not get any properties from the themes.-->
            <Setter Property="OverridesDefaultStyle" Value="true"/>
                <Setter Property="Background" Value="Yellow"/>
                <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid>
                                <Border TextBlock.Foreground="{TemplateBinding Foreground}"
                x:Name="Border"
                CornerRadius="10"
                BorderThickness="1"/>
                                    <ContentPresenter HorizontalAlignment="Center"
                            VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        </Grid.Resources>
        <Button Name="test" Width="50" Height="60" Content="myvalue">
            <Button.Template>
                <ControlTemplate>
                    <Grid>
                        <Ellipse Fill="{TemplateBinding Background}"/>
                        <ContentPresenter HorizontalAlignment="Center"
                            VerticalAlignment="Center"/>                       
                    </Grid>
                </ControlTemplate>
            </Button.Template>
        </Button>
    </Grid>
4

1 に答える 1

1

You should set TargetType in ControlTemplate to Button.

<ControlTemplate TargetType="Button">
    <Grid>
        <Ellipse Fill="{TemplateBinding Background}"/>
        <ContentPresenter HorizontalAlignment="Center"
                             VerticalAlignment="Center"/>                       
    </Grid>
</ControlTemplate>

Explanation (source msdn):

If you have a standalone ControlTemplate in the resources section with the TargetType property set to a type, the ControlTemplate does not get applied to that type automatically. Instead, you need to specify an x:Key and apply the template explicitly.

Also note that the TargetType property is required on a ControlTemplate if the template definition contains a ContentPresenter.

于 2013-01-19T16:39:41.023 に答える