0

私がやろうとしているのは、XAML の代わりに C# を使用して ContentTemplate のコードを記述することです。私が持っているXAMLコードは次のとおりです。

<phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Key="AnyButton">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="140"/>
                    <ColumnDefinition Width="310"/>
                </Grid.ColumnDefinitions>
                <Image Grid.Column="0" Source="/Images/AnyImage.png" Height="80" Width="80" HorizontalAlignment="Left" Margin="15,0,0,0"/>
                <TextBlock Grid.Column="1" TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Left" Text="AnyText" FontSize="30" FontFamily="{StaticResource PhoneFontFamilySemiLight}" Margin="0,0,0,0"/>
            </Grid>
        </DataTemplate>
</phone:PhoneApplicationPage.Resources>

ContentPanel 内で ContentTemplate を使用するために使用したコードは次のとおりです。

<Button x:Name="MyButton" 
        Click="MyButton_Click" 
        ContentTemplate="{StaticResource AnyButton}" 
        Width="492" Height="130" 
        Margin="6,0,6,-6" 
        Background="#003d0a"/>

さて、私の質問は、C# を使用してコード全体を記述することは可能ですか?

4

1 に答える 1

2

スタイリング目的でそれを実行しようとしている場合は、次の方法で実行します。

<Style x:Key="MyButton" TargetType="Button">
            <Setter Property="Width" Value="460"></Setter>
            <Setter Property="Background" Value="/Images/AnyImage.png"></Setter>
        </Style>

これを app.xaml または PhoneApplicationPage.resources で定義し、xaml のボタン コントロールでスタイルを MyButton に設定します。例えば:

<Button style={StaticResource MyButton}/>

画像の幅と高さも定義したい場合は、値セクションでスタイルを定義することにより、同じ方法でそのスタイルで値を設定できます。例えば:

<style x:key ="MyImage" TargetType="Image">
  <setter property="Width" Value="150"/>
</style>

私のボタンスタイルで background value ={StaticResource MyImage} を定義します

于 2013-12-28T17:24:15.133 に答える