简体   繁体   中英

WPF: How works HorizontalAlignment and HorizontalContentAlignment in parent and child control?

I am trying to understand how HorizontalAlignment and HorizontalContentAlignment will work with regard to parent control.

For example, here is simple code:

<StatusBar VerticalAlignment="Bottom" Width="600" HorizontalContentAlignment="Right"  HorizontalAlignment="Left">
  <Label Margin="5">StatusBar</Label>
</StatusBar>

I suppose that the Label will appear at the right side of the StatusBar, because in MCTS 70-511 book I found that HorizontalContantAlignment "Gets or sets the horizontal alignment of the control's content." Isn't the Label is a content in my case? If it is not, so what is?

I have tried to add HorizontalAlignment="Right" to my Label but it made no changes.

Moreover, what will happen if my parent control has HorizontalContentAligment="Left" and the content of it will has HorizontalContentAligment="Right" ? Child's property will "cover" the one from parent?

HorizontalAlignment="Right" - This will change the Alignment for the Control. HorizontalContentAligment="Left" - This Will change the Alignment for the Content based on its parent.

In your code, StatusBar will be set to Left alignment based on its parent.

Label will be set to Right based on the StatusBar .

http://msdn.microsoft.com/en-us/library/vstudio/aa970569(v=vs.100).aspx

With the reference of the above like, StatusBar wasnt use the HorizontalContentAligment Property in its Control Template, so it will not reflect in your Code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM