locked
Drawing a line at runtime with XAML user control RRS feed

  • Question

  • Hey folks,

    I'm new to XAML and trying to write some simple user controls that render a line at runtime on the form. The XAML for the user control defines a single line at design time, then at run time adjusts the line's coordinates and selects a brush stroke to draw it with. When I run the program the control containing the line seems to be clipped unless the window is sized quite a bit larger. The images below show how the line is rendered with the window sized two different ways. Ignore the dots, those are another user control that is rendering correctly. Above the images is the XAML and the C# code that changes the line at runtime. Any help appreciated, or if I'm barking up the wrong tree and there is a better way to do this altogether I'd appreciate knowing that as well.

    thanks!

    <UserControl x:Class="GraphControls.EdgeControl"
                 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="200" d:DesignWidth="200">
        
        <Grid x:Name="LayoutRoot" Height="500" Width="500" Background="Transparent">
            <Line X1="0" Y1="100" X2="100" Y2="0" Stroke="Gray" StrokeThickness="2" />
        </Grid>
    </UserControl>
    
    
            private void Init(Edge edge)
            {
    
                ...............
    
                this.HorizontalAlignment = HorizontalAlignment.Left;
                this.VerticalAlignment = VerticalAlignment.Top;
                
                this.Margin = new Thickness(
                    su.DEFAULT_SCALE * m_edge.Vertex0.X, 
                    su.DEFAULT_SCALE * m_edge.Vertex0.Y, 
                    su.DEFAULT_SCALE * m_edge.Vertex1.X, 
                    su.DEFAULT_SCALE * m_edge.Vertex1.Y 
                );
    
                Line line = ((Line)this.LayoutRoot.Children[0]);
    
                line.X1 = line.Y1 = 0;
                line.X2 = 50;
                line.Y2 = 25;
                line.Stroke = new SolidColorBrush(su.ColorFromString("Gray"));
                line.StrokeThickness = m_edge.Weight;
    
                .............
            }





    Monday, March 5, 2012 2:53 PM