none
Alignment vs ContentAlignment RRS feed

  • Question


  • From microsoft.public.windows.developer.winfx.avalon


    Consider a Window W, Button B, and TextBlock T where

    W.Content = B
    and
    B.Content = T


    T may be aligned inside B by setting B.HorizontalContentAlignment and
    B.VerticalContentAlignment. However T.HorizontalAlignment and
    T.VerticalAlignment have no effect.

    B may be aligned inside W by setting B.HorizontalAlignment and
    B.VerticalAlignment. W.HorizontalContentAlignment and
    W.VerticalContentAlignment have no effect.


    Is there a rule that may be deployed to determine whether these
    properties should be set on the parent (using
    HorizontalContentAlignment and VerticalContentAlignment) or on the
    child (using HorizontalAlignment and VerticalAlignment)? It seems a bit
    unfair to have to guess. The documentation is not very helpful in this
    regard.


    {example code follows }

    Code Block

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;

    namespace AlignmentQuestions {
      class MainWindow : Window {

        [STAThread]
        public static void Main() {
          new Application().Run(new MainWindow());
        }

        public MainWindow() {
          Width = 2 * 96; Height = 2 * 96;
          Button button = new Button();
          TextBlock text_block = new TextBlock(new Run("Button"));
          button.Content = text_block;
          this.Content = button;

          //align text in button 
          //works
          button.VerticalContentAlignment = VerticalAlignment.Top;

          //does not work
          //text_block.VerticalAlignment = VerticalAlignment.Top;
         

          //align button in window
          //does not work
          //HorizontalContentAlignment = HorizontalAlignment.Left;

          //works
          button.HorizontalAlignment = HorizontalAlignment.Left;
        }
      }
    }






    --

    Martin
    Wednesday, October 3, 2007 8:55 AM

Answers

  • You would use HorzontalContentAlignment and VerticalContentAlignment on the Parent when the Parent is a Control, such as a Button.  These all inherit and from ContentControl*, which exposes ContentAlignment properties, and will apply these properties to its content unless the contents behavior is overridden in a Style.

     

    HorizontalAlignment and VerticalAlignment would generally be used on a Child when this child is inside a Panel, such as a Grid.  The Panel will take these properties into account when arranging its children. 

     

    * Window also inherits from ContentControl, but does not utilize these properties in its default implementation.  In its default state, you could assume that the Window will act like a Panel in regards to child alignments.  Set the HorizontalAlignment and VerticalAlignment on the Child of Window.

    Wednesday, October 3, 2007 8:24 PM
  • Container

    Element

    Content

     

    Element's position is affected by the HorizontalAlignment and VerticalAlignment set on Element

    Content's position if affected by the HorizontalContentAlignment and VerticalContentAlignment set on Element

     

     

    --Ifeanyi Echeruo [MSFT]
    Wednesday, October 3, 2007 10:38 PM

All replies

  • You would use HorzontalContentAlignment and VerticalContentAlignment on the Parent when the Parent is a Control, such as a Button.  These all inherit and from ContentControl*, which exposes ContentAlignment properties, and will apply these properties to its content unless the contents behavior is overridden in a Style.

     

    HorizontalAlignment and VerticalAlignment would generally be used on a Child when this child is inside a Panel, such as a Grid.  The Panel will take these properties into account when arranging its children. 

     

    * Window also inherits from ContentControl, but does not utilize these properties in its default implementation.  In its default state, you could assume that the Window will act like a Panel in regards to child alignments.  Set the HorizontalAlignment and VerticalAlignment on the Child of Window.

    Wednesday, October 3, 2007 8:24 PM
  • Container

    Element

    Content

     

    Element's position is affected by the HorizontalAlignment and VerticalAlignment set on Element

    Content's position if affected by the HorizontalContentAlignment and VerticalContentAlignment set on Element

     

     

    --Ifeanyi Echeruo [MSFT]
    Wednesday, October 3, 2007 10:38 PM
  •  

    Dear Matt,

    Thanks for the reply, which I have interpreted as meaning:

     

    "Parent.ContentAlignment overrules Child.Alignment, except if the Parent is a Window."

     

    The question presumably does not arise in the first place if the parent is not derived from ContentControl.

     

    Thursday, October 4, 2007 9:20 PM
  •  

    Yeah that is correct.  If your parent is a ContentControl, and this ContentControl's template uses this property, then the Parent.ContentAlignment will overrule Child.Alignment.  So it all depends on the ContentControls template, which you have full control over. 

    This is a pretty basic look at how button template would use the content aligment property, and overrule the child's aligment property.

     

    Code Block

    <Button HorizontalContentAlignment="Left">

      <Button.Style>
        <Style TargetType="{x:Type Button}">
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate>
                <Border Background="{TemplateBinding Background}">
                  <ContentPresenter

                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"

                    Content="{TemplateBinding Button.Content}"/>
                </Border>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      </Button.Style>

      <Button.Content>
        <Grid Height="200" Width="200" Background="blue" HorizontalAlignment="Right" />
      </Button.Content>

    </Button>

     

     

     

    Thursday, October 4, 2007 9:52 PM