Jump to content

  • Log In with Google      Sign In   
  • Create Account


WPF FrameworkElement Properties


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 maya18222   Members   -  Reputation: 191

Like
0Likes
Like

Posted 09 April 2012 - 04:07 AM

The WPF FrameWorkElement and derived classes have two properties

-HorizontalAlignment - Gets or sets the horizontal alignment characteristics applied to this element when it is composed within a parent element
-HorizontalContentAlignment - 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

Sponsor:

#2 paws1111   Members   -  Reputation: 122

Like
0Likes
Like

Posted 09 April 2012 - 05:01 PM

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?


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. Posted Image

#3 maya18222   Members   -  Reputation: 191

Like
0Likes
Like

Posted 10 April 2012 - 09:02 AM

Thankyou




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS