Answered How to get the margin value of control?

  • Monday, February 27, 2012 8:43 AM
     
     

    In my metro, I set into the relative location of the control, in this time, i try to get the margin value of control, but the result is always zero.

    How to i should do?

All Replies

  • Monday, February 27, 2012 9:53 AM
     
     Answered Has Code

    How are you setting the relative coordinates of the control?

    If you set the margin like this in xaml

    <Button Content="click" Margin="6" x:Name="myButton"></Button>

    then you can retrieve the value in code like this:

                Thickness mg = myButton.Margin;
                double left = mg.Left;
                double right = mg.Right;
                double top = mg.Top;
                double bottom = mg.Bottom;
    


    http://babaandthepigman.spaces.live.com/blog/

  • Monday, February 27, 2012 11:06 AM
     
      Has Code

    <Button Content="click" x:Name="myButton"  HorizontalAlignment="Left"  VerticalAlignment="Top"></Button> I use this style.

  • Monday, February 27, 2012 9:01 PM
    Moderator
     
     Answered Has Code

    Hi Roy,

    If you don't set the margin then I would expect it to be zero.

    What is your real goal here? Are you looking for something other than the margin? If you are looking for the location of the control it is often set by an attached property from the container control, such as Canvas.LeftProperty or Grid.RowProperty. These can be queried from the button with GetValue:

    int x = myButton.GetValue(Canvas.LeftProperty);
    int y = myButton.GetValue(Canvas.TopProperty);

    --Rob

  • Tuesday, February 28, 2012 2:11 AM
     
     

    Hi Rob,  i found that still can't get the value. My code is as follows:

    XML:

    <Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
            <Button Content="Button" HorizontalAlignment="Right" Height="54" VerticalAlignment="Bottom" Width="168" Background="#00E47D7D" x:Name="XBtn"/>
    </Grid>

    c#:

    public BlankPage()
    {
                this.InitializeComponent();
                Debug.WriteLine("=================>>" + XBtn.GetValue(Canvas.LeftProperty));
    }

    The result is still zero.

  • Tuesday, February 28, 2012 4:52 AM
    Moderator
     
     Answered

    Hi Roy,

    Zero is still expected: your button isn't in a Canvas and doesn't have the Left property set. If the location isn't set then you cannot read it out.

    --Rob

  • Tuesday, February 28, 2012 6:57 AM
     
     

    If your Button is inside of a grid - setting Canvas.Left on it has no effect and the default value for Canvas.LeftProperty is 0, so that is what you get.

    You can access the Margin property in your BlankPage constructor by just getting XBtn.Margin value, but in your case it will still return the default Thickness-type value (left: 0, top: 0, right: 0, bottom: 0), since you never set it to anything.

    If you put your button inside of a single-cell Grid  - the button will be positioned based on Horizontal/Vertical-Alignment as well as the Margin. You can replace your Grid with a Canvas and then use Canvas.Left/Canvas.Top for controlling the layout.


    Filip Skakun

  • Wednesday, February 29, 2012 1:42 AM
     
     

    <Grid>

    <ButtonContent="click"x:Name="myButton"  HorizontalAlignment="Left"  VerticalAlignment="Top"></Button>

    </Grid>

    if this is the case, i can't get the position value of myButton?

    Other way?

  • Monday, March 05, 2012 11:12 PM
    Moderator
     
     Answered

    Hi Roy,

    What exactly do you mean by position? Why do you need this? What do you plan to do with the position once you find it?

    In order to get the position the button needs to be in a control which supports position and then have a position set on it that can be queried.

    Depending on why exactly you need the position you may want to look at TransformToVisual instead.

    --Rob