Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I want to draw a huge number of line in a canvas uniformly spaced from top to bottom, (will have left to right also, when I've med it work one way). My approach might be all wrong, or there are some error I can't find. Please review my code and see if you can find it. I have also included the detailed error message:

C# code, (the offending part; executed at program start):
C#
public void createGrid()
{
    int Ypositions = -1000;

    while (Ypositions < 1001)
    {
        // Create a white Brush
        SolidColorBrush whiteBrush = new SolidColorBrush();
        whiteBrush.Color = Colors.White;
        
        // Add line
        Line gridLine = new Line();
        gridLine.X1 = 1;
        gridLine.Y1 = 1;
        gridLine.X2 = 1;
        gridLine.Y2 = 2000;
        gridLine.SetValue(Canvas.LeftProperty, 1);
        gridLine.SetValue(Canvas.TopProperty, Ypositions);
        gridLine.Stroke = whiteBrush;
        gridLine.StrokeThickness = 1;
        gridLine.SetValue(Panel.ZIndexProperty, -900);
        
        // Increment by ten
        Ypositions += 10;

        gridPattern.Children.Add(gridLine);
    }     
}

Let me make myself completely clear, if I don't execute this snippet of code the program works flawlessly!

XAML code:
HTML
<TabItem Header="Starmap" Name="tab_itm_starmap">
    <Grid>
        <Canvas removed="Black" Panel.ZIndex="-1500" />
        <Canvas Name="gridPattern"
                VerticalAlignment="Center"
                HorizontalAlignment="Center"
                removed="Blue"
                Height="1px"
                Width="1px"
                Panel.ZIndex="-1100">
            <!-- Function createGrid() places Lines here on program startup -->
        </Canvas>
    </Grid>  
</TabItem>


Detailed Error message:
System.Windows.Markup.XamlParseException was unhandled
  Message='The invocation of the constructor on type 'WpfApplication1.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'.
  Source=PresentationFramework
  LineNumber=3
  LinePosition=9
  StackTrace:
       at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
       at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
       at System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc)
       at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties)
       at System.Windows.Application.DoStartup()
       at System.Windows.Application.<.ctor>b__1(Object unused)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at WpfApplication1.App.Main() in C:\Users\Frank Haugen\Documents\Visual Studio 2010\Projects\WpfApplication1\WpfApplication1\obj\x86\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.ArgumentException
       Message='0' is not a valid value for property 'Left'.
       Source=WindowsBase
       StackTrace:
            at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
            at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
            at WpfApplication1.MainWindow.createGrid() in C:\Users\Frank Haugen\Documents\Visual Studio 2010\Projects\WpfApplication1\WpfApplication1\MainWindow.xaml.cs:line 107
            at WpfApplication1.MainWindow..ctor() in C:\Users\Frank Haugen\Documents\Visual Studio 2010\Projects\WpfApplication1\WpfApplication1\MainWindow.xaml.cs:line 27
       InnerException: 


Thanks very much for any and all help!
Posted
Updated 30-Mar-12 12:25pm
v2
Comments
Frank R. Haugen 30-Mar-12 19:53pm    
an hour of tinkering have it down to these lines of code:
gridLine.SetValue(Canvas.LeftProperty, 1);
gridLine.SetValue(Canvas.TopProperty, Ypositions);
So maybe I can get it fixed tomorrow as this young Norwegian is going to bed before I fall asleep at the keyboard :/
fct2004 31-Mar-12 23:04pm    
Line objects have an X1, X2, Y1, and Y2 properties. I had to do this for work, and using those properties to set the endpoints of the line worked pretty well. I have to warn anything in the negatives might not be visible because a lot of controls don't render things that are outside of them. And, you might want to increase the width and height of the canvas a bit.

1 solution

WPF - How can I make a brush that paints graph-paper-like squares?[^]

As i understand you want to draw a grid.
I am using approach that posted above, for drawing ECG like paper in my application. DrawingBrush is what you need, draw 2 lines and then use the TileMode="Tile" property.

TileBrush Class[^]

I hope it's helps.
Roman
 
Share this answer
 
v2
Comments
Frank R. Haugen 1-Apr-12 17:40pm    
I didn't know about this, and it's working perfectly. Thank you VERY much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900