2016-02-29 78 views
0

我有這個xaml文件:(C#)需要添加一些XAML編程

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

而且我用另一個xaml文件AccountInfo財產:(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> 

不過,現在我需要在我的代碼中添加AccountInfos,因爲它們的數量每次都有所不同。我如何將它們添加到我的代碼中,而不是在xaml

回答

0

有兩種方法可以做到這一點:

  1. 創建一個用戶控件,它在.xaml.cs添加到StackPanel的像下面

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

  2. 有無一個itemcsontrol而不是stackpanel,itemspanel中的itemcontrol包含了你的usercontrol和itemcontrol的itemssource綁定到一些LISTOFACCOUNTS集合。

+1

它的工作原理!謝謝 :) –