문제

What I am trying to do is use C# to write the code of ContentTemplate instead of XAML. Here is the XAML code that I have:

<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>

And here is the code that I used inside my ContentPanel to use the ContentTemplate:

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

Now, my question is, is it possible to write the whole code using C#?

도움이 되었습니까?

해결책

if you are trying to do it in styling purpose do it in the following way:

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

define this in app.xaml or PhoneApplicationPage.resources and in your button control in xaml set your style to MyButton. for example:

<Button style={StaticResource MyButton}/>

if you also want to define width and height for your image you can do it same way by defining style in value section set the value with that style. for example:

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

in my button style define background value ={StaticResource MyImage}

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top