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

  • Home
  • SEARCH
  • 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 6687993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:21:10+00:00 2026-05-26T05:21:10+00:00

I’m playing with a basic training device as an intro to WPF, where the

  • 0

I’m playing with a basic training device as an intro to WPF, where the user is presented with a column of items on the left of the screen, and one on the right. Their task is to draw lines connecting related items on the left to items on the right. I have started with a very simple 3 column Grid with Rectangles down the left and right columns, with nothing in the centre column, and a simple static Line between two rectangles (I don’t think I did too bad for a first attempt, without Google), but when I try and set the line to Stretch, it starts in the top left corner of the canvas, not on the locus of the Origin. Without stretch it is correctly positioned, but remains static during grid resizing. Here is my attempt:

private Line _line;
private void DrawLink(int row1, int col1, int row2, int col2)
{
    // Find the locus of the Origin rectangle.
    var orig = (Rectangle)grid.Children[row1 * ColumnCount + col1];
    var x1 = orig.ActualWidth - orig.ActualHeight / 2 / 2;
    var y1 = orig.ActualHeight / 2;
    var gp1 = orig.TranslatePoint(new Point(x1, y1), grid);

    // Find the locus of the Target rectangle.
    var targ = (Rectangle)grid.Children[row2 * ColumnCount + col2];
    var x2 = targ.ActualHeight / 2 / 2;
    var y2 = targ.ActualHeight / 2;
    var gp2 = targ.TranslatePoint(new Point(x2, y2), grid);

    _line = new Line
                {
                    Stroke = new SolidColorBrush(Colors.Red),
                    StrokeThickness = 2,
                    // Think this isn't working because line is added dynamically.
                    Stretch = Stretch.Fill,
                    X1 = gp1.X,
                    Y1 = gp1.Y,
                    X2 = gp2.X,
                    Y2 = gp2.Y
                };
    gridCanvas.Children.Add(_line);
}
  • 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-05-26T05:21:11+00:00Added an answer on May 26, 2026 at 5:21 am

    If you choose Stretch.Fill you can only control whether the line is horizontal, vertical, diagonal falling, or diagonal rising, however you could position the end points differently, e.g. by using the Margin. To make the line resize with its container you need to choose a panel which transforms its children, a Canvas is not very suitable in this case, you might be able to use a Grid instead.

    Here is full example which illustrates the above points, note the Margins and the setting of either Y1 or Y2 on the Lines:

    <Page
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <Page.Resources>
        <Style TargetType="Rectangle">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="20"/>
        </Style>
      </Page.Resources>
      <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
          <Grid>  
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition />
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Rectangle Grid.Column="0" Grid.Row="0" Fill="Red"/>
            <Rectangle Grid.Column="0" Grid.Row="1" Fill="Yellow"/>
            <Rectangle Grid.Column="0" Grid.Row="2" Fill="Green"/>
            <Rectangle Grid.Column="0" Grid.Row="3" Fill="Blue"/>
            
            <Line Y2="1" X2="1" Margin="0,10,0,30" Stretch="Fill" Stroke="Red" Grid.RowSpan="4" Grid.Column="1"/>
            <Line Y1="1" X2="1" Margin="0,10,0,50" Stretch="Fill" Stroke="Yellow" Grid.RowSpan="4" Grid.Column="1"/>
            <Line Y2="1" X2="1" Margin="0,50,0,10" Stretch="Fill" Stroke="Green" Grid.RowSpan="4" Grid.Column="1"/>
            <Line Y1="1" X2="1" Margin="0,30,0,10" Stretch="Fill" Stroke="Blue" Grid.RowSpan="4" Grid.Column="1"/>
            
            <Rectangle Grid.Column="2" Grid.Row="2" Fill="Red"/>
            <Rectangle Grid.Column="2" Grid.Row="0" Fill="Yellow"/>
            <Rectangle Grid.Column="2" Grid.Row="3" Fill="Green"/>
            <Rectangle Grid.Column="2" Grid.Row="1" Fill="Blue"/>
          </Grid>
      </ScrollViewer>
    </Page>
    

    If the content resizes dynamically on the vertical axis as well you might need to approach it differently. For example you could then wrap the lines in a ContentControl which starts at the respective lower row and only spans the rows it crosses, it would contain the Line which then has the same value for the Top and Bottom parts of the Margin (half the height of one row). To get the proper value the Margin could be bound with a respective converter in place.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.