- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 TextBlock 动态添加到 RelativePanel,但我想不出一种将它们添加到彼此下方的方法。我的目标是动态添加六个 TextBlocks 并相互交替。
它应该看起来像这样:
+---------+
| left |
| right |
| left |
| right |
| left |
| right |
+---------+
我试过 for 循环,但这不起作用,因为它一直在同一个地方添加它们,而不是在前一个的下面。.cs 代码:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
for (int i = 0; i < 3; i++)
{
TextBlock left = new TextBlock()
{
Name = "left",
Text = "left",
Foreground = new SolidColorBrush(Colors.White)
};
TextBlock right = new TextBlock()
{
Name = "right",
Text = "right",
Foreground = new SolidColorBrush(Colors.White),
};
RelativePanel.SetBelow(left, right);
RelativePanel.SetAlignRightWithPanel(left, true);
relativePanel.Children.Add(left);
relativePanel.Children.Add(right);
}
}
.xaml代码:
<ScrollViewer>
<RelativePanel x:Name="relativePanel">
</RelativePanel>
</ScrollViewer>
如果这不可能,是否有其他方法可以实现?提前致谢。
最佳答案
你比较接近 - 问题是对于 for 循环的下一次迭代,你失去了谁是“左”和“右”的上下文 TextBlock
并且你无法在下面设置新的旧的。这里有一个方法来做你需要的:
public void AddTextBoxes(int count)
{
bool left = true;
TextBlock lastAdded = null;
for (int i = 0; i < count; i++)
{
var currentTextBlock = new TextBlock()
{
Name = "textblock" + i.ToString(),
Text = left ? "left" : "right",
Foreground = new SolidColorBrush(Colors.White)
};
if (lastAdded != null)
{
RelativePanel.SetBelow(currentTextBlock, lastAdded);
}
if (!left)
{
RelativePanel.SetAlignRightWithPanel(currentTextBlock, true);
}
relativePanel.Children.Add(currentTextBlock);
left = !left;
lastAdded = currentTextBlock;
}
}
基本上,您会跟踪最后添加的文本框,以便可以将下一个文本框放在它下面,并且您会跟踪需要放置下一个文本框的位置 - 左侧或右侧。
关于c# - 如何将 TextBlocks 动态添加到 RelativePanel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39493934/
我正在尝试将 TextBlock 动态添加到 RelativePanel,但我想不出一种将它们添加到彼此下方的方法。我的目标是动态添加六个 TextBlocks 并相互交替。 它应该看起来像这样: +
我试图通过 XAML 在 VisualState.Setters 中的 Visual State 中更改控件的 RelativePanel 附加属性,但这些属性没有更改,因此我创建了一个依赖属性以通过
我正在尝试将 HorizontalAlignment 和 VerticalAlignment 设置为 Center,以便 RelativePanel 将位于 UWP 应用程序的中间。 Relati
我在 RelativePanel 内的 PivotItem 中有 3 个 WebView,如下所示:
我正在开发一个 UWP 应用程序,我正在使用 Template10。我有一个 TextBlock,在 VisualStateNarrow 我想要它 RelativePanel.AlignVertica
我是 Windows 运行时编程的新手,并决定在 Windows 10 发布之前坚持下去。 我正在尝试设计一个自适应页面 UI,如下所示, 顶部和下方有标题“东西”,我想要两列,每列都有一个文本 bl
我知道如何使用 Grid 和 StackPanel 来实现它,如果没有它可以在这里完成吗? 最佳答案 当然,只需将它与面板左右对齐即可!例如: 水平拉伸(stretch) 如果你想要垂直
我想在其 DataTemplate 中的每个 ListViewItem 中访问和更改 RelativePanel 的 FlowDirection。我试过这个确切的方法,有同样的错误:How do I
我是一名优秀的程序员,十分优秀!