Draw Diagonal Line in WPF Grid

后端 未结 2 1979
暗喜
暗喜 2021-01-04 20:15

I think I am trying to do something relatively simple in WPF, but can\'t for the life of me figure out how; and think I am probably on the verge of overcomplicating it.

2条回答
  •  渐次进展
    2021-01-04 20:36

    I have created a sample to draw line from code behind which will give you more control... I crated a grid which contains canvas in each cell and on canvas load i am creating a path and adding it to same canvas...

    As @ Mathieu Garstecki answer we can achieve this crating path in xaml... if want to add some logic before creating path you can use my answer XAML

    
            
                
                
                
            
            
                
                
                
            
    
            
            
            
            
            
            
            
            
            
    
        
    

    Code Behind

    private void Canvas_Loaded(object sender, RoutedEventArgs e)
            {
                var g = new StreamGeometry();
                var context = g.Open();
                context.BeginFigure(new Point(0, 0), true, true);
                context.LineTo(new Point((sender as Canvas).ActualHeight, (sender as Canvas).ActualWidth), true, true);
                context.Close();
                System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
                path.Data = g;
                path.Stroke = new SolidColorBrush(Colors.Red);
                path.StrokeThickness = 1.4;
                (sender as Canvas).Children.Add(path);
            }
    

    OutPut

    enter image description here

提交回复
热议问题