我有这个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>
我在另一个AccountInfo
文件中使用xaml
属性:(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 :(得分:0)
有两种方法可以做到:
创建一个usercontrol并将其添加到.paaml.cs中的stackpanel,如下所示
var myAccountInfoControl = new AccountInfo();
AccountsInfo.Children.Add(myAccountInfoControl);
或
有一个itemcsontrol而不是stackpanel,itemspanel的itemscontrol包含你的usercontrol和itemscontrol的itemssource绑定到一些LISTOFACCOUNTS集合。