Search code examples
c#xamlwindows-phone-7mobilewindows-phone-8.1

(C#) Need to add some xaml programatically


I have this xaml file:

<UserControl x:Class="SpectroCoin.Controls.AccountInfo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:effects="clr-namespace:SpectroCoin.Effects"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="130" d:DesignWidth="480">
    <UserControl.Resources>
        <Style x:Key="MainInfoStyle" TargetType="TextBlock">
            <Setter Property="FontSize" Value="32"/>
            <Setter Property="VerticalAlignment" Value="Bottom"/>
        </Style>
        <Style x:Key="ReservedInfoStyle" TargetType="TextBlock">
            <Setter Property="FontSize" Value="20"/>
            <Setter Property="VerticalAlignment" Value="Bottom"/>
        </Style>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Margin="0,0,0,0" >
        <StackPanel Orientation="Vertical" Grid.Column="0" Margin="0,0,0,0">
            <TextBlock FontSize="24" Text="Label" x:Name="txtLabel" Margin="0"/>
            <TextBlock FontSize="50" Text="{Binding Account.AvailableStr}" Margin="50,-10,0,0" Foreground="#FFF9AF28"/>
            <TextBlock FontSize="24" Margin="50,-15,0,0" Visibility="{Binding ReservedVisibility}">
                <Run Foreground="Gainsboro" Text="{Binding LocalizedResources.reserved, Source={StaticResource LocalizedStrings}, StringFormat='\{0\} '}"/>
                <Run Foreground="Gainsboro" Text="{Binding Account.ReservedStr}"/>
            </TextBlock>
        </StackPanel>
        <!-- 
             <Border BorderThickness="0"  Grid.Column="0" VerticalAlignment="Center" Height="50" Padding="0,0,0,5">
             <StackPanel Orientation="Horizontal" Grid.Column="0" Margin="5,0,0,0">
             <TextBlock FontSize="32" VerticalAlignment="Bottom" Text="Label" x:Name="txtLabel" Margin="0,0,20,0"/>
             <TextBlock FontSize="32" VerticalAlignment="Bottom" >
             <Run FontSize="32" Text="{Binding Account.Available}"></Run>
             <Run FontSize="20"  Foreground="Gainsboro" Text="{Binding Account.Reserved, StringFormat=' (+{0}'}"/>
             <Run FontSize="20"  Foreground="Gainsboro" Text="{Binding LocalizedResources.reserved, Source={StaticResource LocalizedStrings}, StringFormat='\{0\})'}"/>
             </TextBlock>
 -->
        <!--
                <TextBlock Text="{Binding Account.Available}" Style="{StaticResource MainInfoStyle}"/>
        <TextBlock Text="test" Style="{StaticResource MainInfoStyle}">
            <Run Text="{Binding Account.Available}"></Run>
        </TextBlock>
    </StackPanel>
</Border>-->
</Grid>
</UserControl>

And I use AccountInfo property in another xaml file: (ScreenShot)

<phone:PanoramaItem Header="{Binding LocalizedResources.balance, Source={StaticResource LocalizedString}}">
    <StackPanel Margin="15,0,0,0">
        <StackPanel Name="AccountsInfo">
            <local:AccountInfo x:Name="accountInfoo" Label="{Binding LocalizedResources.Euro, Source={StaticRessource LocalizedStrings}}" Margin="0,0,0,12" Tap="accountInfo_Tap" />
        </StackPanel>
        <local:RateChart x:Name="rateChart" Height="324" Margin="-12,25,0,0" Width="417" />
    </StackPanel>
</phone:PanoramaItem>

However, now I need to add AccountInfos in my code, because number of them varies everytime. How can I add them in my code, not in xaml?


Solution

  • There are two ways you can do that:

    1. create a usercontrol and add it to the stackpanel like below in .xaml.cs

      var myAccountInfoControl = new AccountInfo();
      AccountsInfo.Children.Add(myAccountInfoControl);
      

      or

    2. Have an itemcsontrol instead of stackpanel with itemspanel of itemscontrol containing your usercontrol and itemscontrol's itemssource binded to some LISTOFACCOUNTS collection.