简体   繁体   中英

How to Set Parent Panel Alignment of The Control Programmatically?

Background:

I have created a helper method to set properties of each TextBlock as per its adjacent TextBox for some scenarios in my application. In this method I already have the TextBlock object, the TextBox object and the parent object (it's always RelativePanel in my case). The RelativePanel in my views always contain only 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
            }
        }
    }
}

Relative Panel Sample:

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

Question:

How can I set the following property of TextBlock programmatically:

RelativePanel.AlignLeftWith="UserName"

AlignLeft is an Attached Property and can be found on RelativePanel itself. You set them like this:

RelativePanel.SetAlignLeftWith(element, UserName);

You can read the docs on the property here :

Edit: fixed syntax error based on comment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM