Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8894661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:33:34+00:00 2026-06-14T23:33:34+00:00

I need to create a line with the first point centered to the Grid

  • 0

I need to create a line with the first point centered to the Grid it is contained in (without manually setting Width/Height of Grid). Here is very simple code sample I want to update so I have one point of “Line1” centered within “Grid1”.

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SimpleLine.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="640" Height="480">
    <Grid x:Name="LayoutRoot">
        <Grid Name="Grid1">
            <Line Name="Line1" X2="200" Y2="100" Stroke="Black"></Line>
        </Grid>
    </Grid>
</Window>

When I try in code behind:

Line1.X1 = Grid1.Widht/2; Line1.Y1 = Grid1.Height/2;

I get an error (uncaught exception) – that “Not a number” is not a valid value for X1(Y1).

Thanks for any efforts.

PS: I am rather a WPF-beginner.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:33:36+00:00Added an answer on June 14, 2026 at 11:33 pm

    In code behind you will have to use ActualWidth and ActualHeight if you have not explicitly set the Width/Height.

    Line1.X1 = Grid1.ActualWidth / 2; Line1.Y1 = Grid1.ActualHeight / 2;
    

    if you want to do in XAML you can make BindingConverters to perform logic on a binded value.

    Xaml:

    <Window x:Class="WpfApplication6.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication6"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <local:DivideByConverter x:Key="Divider" />
        </Window.Resources>
        <Grid x:Name="LayoutRoot">
            <Line Name="Line1" X2="{Binding ElementName=LayoutRoot, Path=ActualWidth, Converter={StaticResource ResourceKey=Divider}}" Y2="{Binding ElementName=LayoutRoot, Path=ActualHeight, Converter={StaticResource ResourceKey=Divider}}" Stroke="Black"/>
        </Grid>
    </Window>
    

    Binding converter:

     public class DivideByConverter : IValueConverter
        {
            /// <summary>
            /// Converts a value.
            /// </summary>
            /// <param name="value">The value produced by the binding source.</param>
            /// <param name="targetType">The type of the binding target property.</param>
            /// <param name="parameter">The xmlentry to the language value</param>
            /// <param name="culture">The culture to use in the converter.</param>
            /// <returns>
            /// A converted value. If the method returns null, the valid null value is used.
            /// </returns>
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                int divider = 2;
                if (value is double)
                {
                    return (double)value / divider;
                }
                return value;
            }
    
            /// <summary>
            /// Converts a value.
            /// </summary>
            /// <param name="value">The value that is produced by the binding target.</param>
            /// <param name="targetType">The type to convert to.</param>
            /// <param name="parameter">The converter parameter to use.</param>
            /// <param name="culture">The culture to use in the converter.</param>
            /// <returns>
            /// A converted value. If the method returns null, the valid null value is used.
            /// </returns>
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }
    

    This will take the Binded value (ActualWidth) and divide it by two and the same for ActualHeight.
    So even if your from is resized the line will stay in the center

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a new line of data within an array where the
I need to create a batch file that reads a file with one line
So I need to create a graph that basically looks like an ascending line
I need to create a PHP order form with 150 lines, each line has
Here's the situation: I need to retrieve the face's under point boundaries, but if
I'm new to java and really need some help. I created a command line
I need create custom dialog and put JPanel into it. Is it possible?
i need create an email list sending to many emails. what is best solution
I need create clone repository. but I do not know where can I get
I need create a document word with Java. And I ask, how can I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.