Jump to content
  • Advertisement
Sign in to follow this  

WPF FrameworkElement Properties

This topic is 2750 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

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

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

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"/>

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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!