WPF FrameworkElement Properties

Started by
1 comment, last by maya18222 12 years ago
The WPF FrameWorkElement and derived classes have two properties

-HorizontalAlignment - [color=#000000]Gets or sets the horizontal alignment characteristics applied to this element when it is composed within a parent element
-HorizontalContentAlignment - [color=#000000]Gets or sets the horizontal alignment of the control's content

They seem to be two different places to store the same objective though.For example, you could break it by having a window with a button as content, and setting the buttons Alignment to one thing, and the windows content alignment to something else.

Are they supposed to be kept in sync? or am I misunderstanding something
Advertisement
First of all let me make a slight correction, although HorizontalAlignment is in Framework element the HorizontalContentAlignment is actually in found in the Control class. As you probably know HorizontalAlignment aligns the container in the parent control. On the other hand HorizontalContentAlignment aligns the some child content control in respect to the current control that you are setting the property on.

The actual implementation of the property is based on the controls template, the control itself does not have to respect this property. In the case of the Window class, this seems to be the case the content control always appears to be stretched (which makes sense). There might be little more to it than that (and someone please correct me if I’m wrong) as msdn mentions something about the property being a template parameter, but I think it basically means what i stated above, the actual usage of HorizontalContentProperty is template implementation dependent.


Are they supposed to be kept in sync?[/quote]

The answer is no, under the hood HorizontalContentAlignment is delegating its value to another controls HorizontalAlighnment, and you may be thinking "but i can set the forms grid's horizontal alignment just fine!" but you’re missing part of the visual tree. There is actually a ContentPresenter control that the horizontal alignment is being set on that is just above the control you are creating. The consumer usually is not able to set the property of this control without modifying the template. (hopefully examples bellow will make this easier to understand)


So now you may be wondering, what’s an practical actual example of this in action?

<Textbox HorizontalContentAlignment="Center" Text="Hello World"/>


This centers the text without centering the control, no template modification needed.

One more example

<Button>
<TextBlock HorizontalAlignment="Right" Text="myText"/>
</Button>


If you attempt to right align the button text with the above code, it will stay centered. The reasons for this are because the ContentPresenter control that contains the Textblock control is center aligned, which means its width and height are as small as the TextBlock allows, meaning right alignment is irrelevant to the Textblock, because the ContentPresenter container is sized to fit the control in a minimal fashion.


<Button HorizontalContentAlignment="Right">
<TextBlock Text="myText"/>
</Button>


This above code will do as intended. The container holding the text block will now be told to right align its self, and therefore also right aligning your text block.


This post was much longer than I originally intended, and so I don’t have much time to proof read this post. But I hope I explained this clearly and explains any questions between the difference between these two propertys. smile.png
Thankyou

This topic is closed to new replies.

Advertisement