[英]How to add multiple properties in a Textblock programatically using WPF & c#?
[英]How to copy all properties of a textblock to another textblock in c#?
我在Grid(xaml)中定义了一个文本块visual_name,用作以可视方式将文本块添加到Grid的可视提示
如何将visual_name的所有属性复制到以编程方式创建的textblock(txtblock)?
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock x:Name="visual_name" HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" RenderTransformOrigin="0.419,0.528" Margin="84,187,0,0" Width="220" Foreground="#FF0E0E0E"/>
<TextBlock x:Name="visual_point" HorizontalAlignment="Left" Margin="352,187,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="102" Height="28" Foreground="Black"/>
<TextBlock x:Name="visual_rank" HorizontalAlignment="Left" Margin="10,186,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="56" Height="28">
</TextBlock>
</Grid>
我的C#代码:
foreach(Leaders leader in listofleaders )
{
TextBlock txtblock = new TextBlock();
//copy properties of visual_name to txtblock
txtblock.Text = leader.name;
// add margin
LayoutRoot.Children.Add(txtblock);
}
我认为您想克隆Textblock对象。 也许您可以在System.Windows.Markup
命名空间中尝试XamlWriter
。
下面的代码可能会有所帮助。
用xaml编写textblock并获得一个字符串作为回报。
string visualNameXaml = XamlWriter.Save(visual_name);
然后,您可以使用以下代码进行检索。
StringReader stringReader = new StringReader(visualNameXaml);
XmlReader xmlReader = XmlReader.Create(stringReader);
TextBlock newTextBlock = (TextBlock)XamlReader.Load(xmlReader);
之后,您可以动态添加文本块。 希望这可以帮助!。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.