C# 如何以编程方式设置控件的父面板对齐方式?
背景: 我创建了一个助手方法,用于为我的应用程序中的某些场景,根据相邻的C# 如何以编程方式设置控件的父面板对齐方式?,c#,xaml,uwp,C#,Xaml,Uwp,背景: 我创建了一个助手方法,用于为我的应用程序中的某些场景,根据相邻的TextBox设置每个TextBlock的属性。在这个方法中,我已经有了TextBlock对象、TextBox对象和父对象(在我的例子中总是RelativePanel)。 我的视图中的RelativePanel始终只包含TextBox&TextBlock // Helper Method public static void SetPropertiesOfEachTextBlock(IList<TextBox>
TextBox
设置每个TextBlock
的属性。在这个方法中,我已经有了TextBlock
对象、TextBox
对象和父对象(在我的例子中总是RelativePanel
)。
我的视图中的RelativePanel
始终只包含TextBox
&TextBlock
// Helper Method
public static void SetPropertiesOfEachTextBlock(IList<TextBox> boxes)
{
foreach (TextBox textBox in boxes)
{
var parent = VisualTreeHelper.GetParent(textBox) as RelativePanel;
foreach(var element in parent.Children)
{
if(element is TextBlock)
{
TextBlock textBlock = (TextBlock)element;
textBlock.FontSize = textBox.FontSize;
textBlock.Margin = textBox.Margin;
textBlock.FontWeight = textBox.FontWeight;
textBlock.Foreground = new SolidColorBrush(Colors.Gray);
// Here I need to set alignment to the adjacent TextBox by the RelativePanel
}
}
}
}
AlignLeft
是一个附加属性,可以在RelativePanel
本身上找到。您可以这样设置它们:
RelativePanel.SetAlignLeftWith(element, UserName);
您可以阅读该属性上的文档:
编辑:修复了基于注释的语法错误
AlignLeft
是一个附加属性,可以在相对面板
本身上找到。您可以这样设置它们:
RelativePanel.SetAlignLeftWith(element, UserName);
您可以阅读该属性上的文档:
编辑:修复了基于注释的语法错误此setter是静态的。。这对我有用:
RelativePanel.SetAlignLeftWith(textBlock,textBox)代码>此setter是静态的。。这对我有用:RelativePanel.SetAlignLeftWith(textBlock,textBox)代码>
RelativePanel.SetAlignLeftWith(element, UserName);