如何以编程方式设置控件的父面板对齐?

时间:2017-07-17 09:04:13

标签: c# xaml uwp

背景:

对于我的应用程序中的某些场景,我已经创建了一个辅助方法来根据其相邻TextBlock设置每个TextBox的属性。在这个方法中,我已经拥有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
            }
        }
    }
}

相对小组样本:

         <RelativePanel>
            <TextBox Name="UserName"/>
            <TextBlock RelativePanel.AlignLeftWith="UserName" />
        </RelativePanel>

问题:

如何以编程方式设置TextBlock的以下属性:

RelativePanel.AlignLeftWith="UserName"

1 个答案:

答案 0 :(得分:2)

AlignLeft是一个附加属性,可以在RelativePanel本身找到。你这样设置:

RelativePanel.SetAlignLeftWith(element, UserName);

您可以阅读属性here上的文档:

编辑:根据评论

修复语法错误