3

I am adding to a CavnasObject (Size 0, 0) a Line Element in Code. But it gets not displayed. So I added the same line object in xaml with same Properties. In XAML it gets displayed but not in C#. Here is my code in C#:

Line line = new Line() { X1 = points[0].X, Y1 = points[0].Y, X2 = points[1].X, Y2 = points[1].Y, Stroke = new SolidColorBrush(Colors.Red), StrokeThickness = 4, Visibility = System.Windows.Visibility.Visible }; lineCanvas.Children.Add(line); 

The is not shown. And here is the line shown:

<StackPanel Grid.Column="1"> <Grid Height="0"> <Canvas Name="lineCanvas"> <Line X1="1" X2="240" Y1="33" Y2="33" StrokeThickness="4" Stroke="Red"/> </Canvas> </Grid> <DataGrid Name="dataGrid" Grid.Column="0" ItemsSource="{Binding ViewMap}" CanUserReorderColumns="False" CanUserSortColumns="False" AutoGenerateColumns="False" AllowDrop="True" DragEnter="dataGrid_DragEnter" Drop="dataGrid_Drop" SelectionUnit="FullRow" HeadersVisibility="Column" IsReadOnly="True" Panel.ZIndex="0" MouseDown="dataGrid_MouseDown"> <DataGrid.CellStyle># .... ... 

If you are yourself why Height=0 of the Grid. I am connecting datacells of a datagrid with lines. And to place to lines I am using a very small canvas object and i am simply displaying lines out of the canvas.

So does anyone has an idea whats wrong. The Values of the XAML are taken of the code.

1
  • 1
    Are you sure that the values of points[0] and points[1] match the values you used in the Xaml? Commented Jul 10, 2012 at 15:20

1 Answer 1

1

Ok after testing I figured out that your XAML might be the problem. Your Canvas is created in the Visual Tree behind the DataGrid so any Line will be shown behind the DataGrid. And since you use a StackPanel as root element I'm not sure where your Line will end up. To fix the issue rewrite your XAML to something similar to this:

<Grid Grid.Column="1"> <DataGrid/> <Canvas x:Name="lineCanvas"/> </Grid> 
Sign up to request clarification or add additional context in comments.

1 Comment

The XAML in the question works as the Panel.ZIndex is set manually to override this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.