Sign in to follow this  
maya18222

WPF FrameworkElement Properties

Recommended Posts

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[/color]
-HorizontalContentAlignment - [color=#000000]Gets or sets the horizontal alignment of the control's content[/color]

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

Share this post


Link to post
Share on other sites
First of all let me make a slight correction, although HorizontalAlignment is in Framework element the HorizontalContentAlignment is actually in found in the [url="http://msdn.microsoft.com/en-us/library/system.windows.controls.control.horizontalcontentalignment.aspx"]Control [/url]class. As you probably know HorizontalAlignment aligns the container in the [b]parent [/b]control. On the other hand HorizontalContentAlignment aligns the some [b]child content control [/b]in respect to the [b]current control[/b] 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.


[quote]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?
[CODE]
<Textbox HorizontalContentAlignment="Center" Text="Hello World"/>
[/CODE]

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

One more example
[CODE]
<Button>
<TextBlock HorizontalAlignment="Right" Text="myText"/>
</Button>
[/CODE]

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.

[CODE]
<Button HorizontalContentAlignment="Right">
<TextBlock Text="myText"/>
</Button>
[/CODE]

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. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this