(C#)需要以编程方式添加一些 xaml

本文关键字:添加 方式 xaml 编程 | 更新日期: 2023-09-27 18:36:51

我有这个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属性:(屏幕截图)

<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

(C#)需要以编程方式添加一些 xaml

有两种

方法可以做到这一点:

  1. 创建一个用户控件并将其添加到堆栈面板中,如下所示的 .xaml .cs

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

  2. 有一个 itemcsontrol 而不是堆栈面板,其中包含包含您的用户控件和 itemscontrol 的 itemssource 绑定到某个 LISTOFACCOUNTS 集合的项目面板。