Reputation: 5994
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.
Upvotes: 3
Views: 3833
Reputation: 2977
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>
Upvotes: 1