Moamen Naanou
Moamen Naanou

Reputation: 1713

How to Set Parent Panel Alignment of The Control Programmatically?

Background:

I have created a helper method to set properties of each TextBlockas 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"

Upvotes: 0

Views: 478

Answers (1)

Jon G St&#248;dle
Jon G St&#248;dle

Reputation: 3904

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

Upvotes: 2

Related Questions