Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何以编程方式设置控件的父面板对齐方式?_C#_Xaml_Uwp - Fatal编程技术网

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);