1

ここで概説されている例を実装しようとしています:

http://www.codeproject.com/Articles/30994/Introduction-to-WPF-Templates

作成者は、「このContentPresenterコントロールを使用して、WPF コントロールのコンテンツを表示できます」と述べています。

次のコードを使用します。

<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" 
Content="{TemplateBinding Button.Content}" />

次のようにウィンドウに追加しました。

<Window x:Class="HKC.Desktop.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="487" Width="765.924" Loaded="Window_Loaded">

    <Grid x:Name="mainGrid" Background="#FF252525">
        <Button Content="Push Me" Template="{StaticResource buttonTemplate}" Name="button1" Height="100" Width="100"></Button>

        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"  Content="{TemplateBinding Button.Content}" />
    </Grid>


</Window>

しかし、次のエラーが発生します。

Cannot set a TemplateBinding if not in a template.

どうすればこれを解決できますか?

4

2 に答える 2

1

そのように、ContentPresent を ControlTemplate に入れる必要があります。

<ControlTemplate x:Key="buttonTemplate" TargetType="{x:Type Button}">
            <Grid>
                <Ellipse Name="el1" Fill="Orange" Width="100" Height="100">
                </Ellipse>
                <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" 
                        Content="{TemplateBinding Button.Content}" />
            </Grid>
</ControlTemplate>
于 2012-04-04T13:23:38.697 に答える
0

問題は、テンプレートがないことです。XAML は次のようになります。

<Window x:Class="HKC.Desktop.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="487" Width="765.924" Loaded="Window_Loaded">
    <Window.Resources>
        <ControlTemplate x:Key="buttonTemplate" TargetType="{x:Type Button}">      
            <Ellipse Name="el1" Fill="Orange" Width="100" Height="100">            
            <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Button.Content}" /> 
        </ControlTemplate>
    </Window.Resources> 

    <Grid x:Name="mainGrid" Background="#FF252525"> 
        <Button Content="Push Me" Template="{StaticResource buttonTemplate}" Name="button1" Height="100" Width="100"/>  
    </Grid> 
</Window> 
于 2012-04-04T13:24:25.670 に答える