0

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

Robert MacLean
  • 38,975
  • 25
  • 98
  • 152
Tanvir Sourov
  • 417
  • 1
  • 6
  • 32
  • What do you want actually is not clear. Do you want to create button control programatically and use the datatemplate in the button?? – jubair Dec 28 '13 at 16:44
  • What I want is add the button inside the constructor of the MainPage.cs class. I am not trying to add it using C# instead of XAML. And yes, the button control will work pragmatically. And I will Bind the ContentTemplate inside the button (for some style purpose). – Tanvir Sourov Dec 28 '13 at 16:57
  • There is a lot samples: http://stackoverflow.com/questions/5755455/how-to-set-control-template-in-code – crea7or Dec 28 '13 at 17:34
  • and here: http://stackoverflow.com/questions/17593999/how-to-create-controltemplate-from-code-behind-in-windows-store-app – crea7or Dec 28 '13 at 17:35
  • Yes, I have seen both of the codes before posting about it. But I am talking about ContentTemplate, not ControlTemplate. Are they both similar thing? @crea7or – Tanvir Sourov Dec 28 '13 at 17:42
  • http://stackoverflow.com/questions/248362/how-do-i-build-a-datatemplate-in-c-sharp-code – crea7or Dec 28 '13 at 17:45

1 Answers1

2

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}

jubair
  • 597
  • 1
  • 6
  • 23