C# 自定义 Canvas class, ActualHeight/Width 始终为 0
C# Custom Canvas class, ActualHeight/Width are always 0
我想实现 class 在 Canvas
上绘制网格,但问题是 ActualWidth
和 ActualHeight
设置为 0。
Canvas 都是灰色地带。
谁能告诉我我做错了什么,我是 C# 的新手。谢谢!
class ImageGrid : Canvas
{
private double GridSize = 50;
public ImageGrid()
{
Background = new SolidColorBrush(Colors.DarkGray);
}
public void DrawGrid()
{
Children.Clear();
#region Draw image
// TODO - Draw image
#endregion
#region Draw grid
for ( double x = 0; x < ActualWidth; x+=GridSize )
{
Line line = new Line();
line.X1 = x;
line.X2 = x;
line.Y1 = 0;
line.Y2 = ActualWidth; // Why 0 ?
line.Stroke = Brushes.Black;
line.StrokeThickness = 1;
Children.Add(line);
}
for ( double y = 0; y < ActualHeight; y += GridSize )
{
Line line = new Line();
line.X1 = 0;
line.X2 = ActualHeight; // Why 0 ?
line.Y1 = y;
line.Y2 = y;
line.Stroke = Brushes.Black;
line.StrokeThickness = 1;
Children.Add(line);
}
#endregion
}
}
ImageGrid 调用:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ImageGrid ig = new ImageGrid();
Grid.Children.Add(ig);
ig.DrawGrid();
}
}
在获取 ActualHeight/Width 之前需要等待布局传递,请尝试在加载的处理程序中调用 ig.DrawGrid。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ImageGrid ig = new ImageGrid();
Grid.Children.Add(ig);
ig.Loaded += ImageGrid_Loaded;
}
void ImageGrid_Loaded(object sender, RoutedEventArgs e)
{
ig.DrawGrid();
}
}
来自 FrameWorkElement.ActualHeight
的注释
This property is a calculated value based on other height inputs, and the layout system. The value is set by the layout system itself, based on an actual rendering pass, and may therefore lag slightly behind the set value of properties such as Height that are the basis of the input change.
Because ActualHeight is a calculated value, you should be aware that there could be multiple or incremental reported changes to it as a result of various operations by the layout system. The layout system may be calculating required measure space for child elements, constraints by the parent element, and so on.
我想实现 class 在 Canvas
上绘制网格,但问题是 ActualWidth
和 ActualHeight
设置为 0。
谁能告诉我我做错了什么,我是 C# 的新手。谢谢!
class ImageGrid : Canvas
{
private double GridSize = 50;
public ImageGrid()
{
Background = new SolidColorBrush(Colors.DarkGray);
}
public void DrawGrid()
{
Children.Clear();
#region Draw image
// TODO - Draw image
#endregion
#region Draw grid
for ( double x = 0; x < ActualWidth; x+=GridSize )
{
Line line = new Line();
line.X1 = x;
line.X2 = x;
line.Y1 = 0;
line.Y2 = ActualWidth; // Why 0 ?
line.Stroke = Brushes.Black;
line.StrokeThickness = 1;
Children.Add(line);
}
for ( double y = 0; y < ActualHeight; y += GridSize )
{
Line line = new Line();
line.X1 = 0;
line.X2 = ActualHeight; // Why 0 ?
line.Y1 = y;
line.Y2 = y;
line.Stroke = Brushes.Black;
line.StrokeThickness = 1;
Children.Add(line);
}
#endregion
}
}
ImageGrid 调用:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ImageGrid ig = new ImageGrid();
Grid.Children.Add(ig);
ig.DrawGrid();
}
}
在获取 ActualHeight/Width 之前需要等待布局传递,请尝试在加载的处理程序中调用 ig.DrawGrid。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ImageGrid ig = new ImageGrid();
Grid.Children.Add(ig);
ig.Loaded += ImageGrid_Loaded;
}
void ImageGrid_Loaded(object sender, RoutedEventArgs e)
{
ig.DrawGrid();
}
}
来自 FrameWorkElement.ActualHeight
的注释This property is a calculated value based on other height inputs, and the layout system. The value is set by the layout system itself, based on an actual rendering pass, and may therefore lag slightly behind the set value of properties such as Height that are the basis of the input change.
Because ActualHeight is a calculated value, you should be aware that there could be multiple or incremental reported changes to it as a result of various operations by the layout system. The layout system may be calculating required measure space for child elements, constraints by the parent element, and so on.