Solved

WPF User Control Polyline Size Wrong

Posted on 2013-12-14
13
516 Views
Last Modified: 2013-12-23
I have created a custom user control for WPF use with VB.net for the code behind.  Essentially, it is a polyline that snaps to connect other custom controls.  The Polyline is created via code at runtime.  It works great vertically and if it has both width and height.  However if it is horizontal or flat, it distorts.  The thicker the line, the worse it distorts.  It distorts on both ends with neither beginning or ending in the right location.

Any ideas on how to fix?  It looks like a stretch issue but it doesn't stretch completely.
0
Comment
Question by:Tesla428
  • 8
  • 5
13 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39721146
Can you give us a small bit of code to highlight the problem, please?
0
 

Author Comment

by:Tesla428
ID: 39727285
Screen shot of the issue
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39727300
What does the XAML/code look like?
0
 

Author Comment

by:Tesla428
ID: 39727404
Public Class Pipe
    Private mLine As New Polyline
    Private mOutline As New Polyline

    Public Sub New(ByVal First As Point, ByVal Last As Point)
        InitializeComponent()

        With mOutline
            .Stroke = Brushes.Red
            .HorizontalAlignment = HorizontalAlignment.Left
            .VerticalAlignment = VerticalAlignment.Top
            .StrokeThickness = 5
            .Stretch = Stretch.None
        End With

        With mLine
            .Stroke = Brushes.Black
            .HorizontalAlignment = HorizontalAlignment.Left
            .VerticalAlignment = VerticalAlignment.Top
            .StrokeThickness = 2
            .Stretch = Stretch.None
         
            If First.X = Last.X Or First.Y = Last.Y Then
                .Points.Add(First)
                .Points.Add(Last)
            End If
            If First.X > Last.X Then
                .Points.Add(First)
                .Points.Add(New Point(First.X, Last.Y))
                .Points.Add(Last)
            End If
            If First.X < Last.X Then
                .Points.Add(First)
                .Points.Add(New Point(First.X, Last.Y))
                .Points.Add(Last)
            End If
        End With

        For Each SharePoint As Point In mLine.Points
            mOutline.Points.Add(SharePoint)
        Next

        grdPipe.Children.Add(mOutline)
        grdPipe.Children.Add(mLine)

    End Sub

End Class

Open in new window


Polyline Class VB Code Behind
0
 

Author Comment

by:Tesla428
ID: 39727406
<UserControl x:Class="Pipe"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" HorizontalAlignment="Left" VerticalAlignment="Top">
    <Grid Name="grdPipe"></Grid> 
</UserControl>

Open in new window


XAML For the Polylines
0
 

Author Comment

by:Tesla428
ID: 39727412
<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Working" Height="362" Width="426" WindowState="Maximized">
    <Grid >
        <DockPanel x:Name="LayoutRoot"             LastChildFill="True">
            <StackPanel  Height="40" DockPanel.Dock="Top" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
                <Slider x:Name="ZoomSlider"                Minimum="1" Value="1" Maximum="5" Height="26" VerticalAlignment="Top" HorizontalAlignment="Left" Width="85" Margin="10,10,0,0" />
                <TextBlock Height="22" Name="TextBlock1" Text="{Binding ElementName=ZoomSlider, Path=Value, StringFormat='Current Zoom: \{0:0.0\}x'}" Margin="12" />
                <CheckBox Content="Show GridLines" Height="16" Name="chkSnapLines" />
                
            </StackPanel>
            <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" HorizontalAlignment="Left" VerticalAlignment="Top">
                <Grid Name="grdMain" VerticalAlignment="Top" HorizontalAlignment="Left">
                    <Grid.LayoutTransform>
                        <ScaleTransform ScaleX="{Binding Path=Value, ElementName=ZoomSlider}"
                                        ScaleY="{Binding Path=Value,ElementName=ZoomSlider}" />
                    </Grid.LayoutTransform>
                   </Grid>
            </ScrollViewer>
        </DockPanel>

    </Grid>
</Window>

Open in new window


XAML for Main Window
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 39727422
I read your initial question, and I see the screen shot, but I still don't quite understand the issue.  Are you having a problem with calculating the length of each line, with respect to the aspect ratio for the drawing surface?
0
 

Author Comment

by:Tesla428
ID: 39727449
No, I need the lines to start and stop at a specific location.  They do this correctly in every instance if they have a vertical component.  However, if the are completely flat they stretch.  Both the start point and end point are both affected and if the line is wider, it gets even worse.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39727516
If you believe that this is a stretch issue, what is the width of the drawing surface, compared to it's height?  If it is square, I wouldn't think that this would be a stretching issue.
0
 

Author Comment

by:Tesla428
ID: 39727546
Are you able to replicate the issue?  Even on a simple polyline?  I am service packing visual studio and loading all available updates.  I don't think it is video driver related.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39727651
You are using a ScaleTransform.  Does this happen when Zoom is at 1.0?
0
 

Assisted Solution

by:Tesla428
Tesla428 earned 0 total points
ID: 39727664
Found it.  Bad Logic in the if/then statements.  Everything renders fine.
0
 

Author Closing Comment

by:Tesla428
ID: 39735710
I was able to locate it on my own.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now