(C#) 需要以编程方式添加一些 xaml
Posted
技术标签:
【中文标题】(C#) 需要以编程方式添加一些 xaml【英文标题】:(C#) Need to add some xaml programatically 【发布时间】:2016-06-12 08:26:20 【问题描述】:我有这个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
?
【问题讨论】:
【参考方案1】:有两种方法可以做到这一点:
创建一个用户控件并将其添加到堆栈面板中,如下面的 .xaml.cs 中
var myAccountInfoControl = new AccountInfo();
AccountsInfo.Children.Add(myAccountInfoControl);
或
有一个 itemcsontrol 而不是 stackpanel,其中 itemscontrol 的 itemspanel 包含您的 usercontrol 和 itemscontrol 的 itemssource 绑定到某个 LISTOFACCOUNTS 集合。
【讨论】:
以上是关于(C#) 需要以编程方式添加一些 xaml的主要内容,如果未能解决你的问题,请参考以下文章