1

我在 XAML 中定义了一个网格,例如,

<Grid Name="myGrid" Grid.Row="0" Grid.Column="0">
     <!-- row/column definitions are defined in c# file -->
</Grid> 

这也是 xaml 文件中其他已定义网格的一部分。

在构造函数的 C# 文件中,在行之后,InitializeComponent()我为行和列定义了网格定义

public State1() {
    InitializeComponent();
    // define the rows
    for (int i = 0; i < 5; i++) {
       RowDefinition rowDef = new RowDefinition();
       rowDef.Height = new GridLength(1, GridUnitType.Star);
       myGrid.RowDefinitions.Add(rowDef);
    }

    // define the columns
    // column for Line
    ColumnDefinition colDef1 = new ColumnDefinition();
    colDef1.Width = new GridLength(8, GridUnitType.Star);
    myGrid.ColumnDefinitions.Add(colDef1);
    // column for TextBlock
    ColumnDefinition colDef2 = new ColumnDefinition();
    colDef2.Width = new GridLength(1, GridUnitType.Star);
    myGrid.ColumnDefinitions.Add(colDef2);

    for (int i = 0; i < 5; i++) {
        // draw dashed line
        Line line= new Line() {
            X2 = myGrid.ColumnDefinitions[0].ActualWidth, // actual width is 0
            Stroke = Brushes.Black, 
            StrokeDashArray = {10, 10, 10, 10},
            StrokeThickness = 1
        };
        Grid.SetRow(line, i);
        Grid.SetColumn(line, 0);
        myGrid.Children.Add(line);
        // draw text
        Viewbox vb_txtBlock = new Viewbox();
        TextBlock txtBlock = new TextBlock();
        txtBlock.Text = "Line " + (i+1);
        vb_txtBlock.Child = txtBlock;
        Grid.SetRow(vb_txtBlock, i);
        Grid.SetColumn(vb_txtBlock, 1);
        myGrid.Children.Add(vb_txtBlock);
    }
}

所以,我的问题是myGrid.ColumnDefinitions[0].ActualWidth返回 0 值。我已经阅读了其他问题和问题,我发现在测量和安排控件之前不会设置 ActualHeight 和 ActualWidth 。

我的问题是,在我的 c# 代码中我可以测量实际宽度和高度,以便我可以使用实际宽度和高度?

4

2 回答 2

2

您应该将初始化代码放在后面InitializeComponent并在事件中获取测量值,Loaded如下所示:

    public Window2()
    {
        InitializeComponent();
        for (int i = 0; i < 5; i++)
        {
            RowDefinition rowDef = new RowDefinition();
            rowDef.Height = new GridLength(1, GridUnitType.Star);
            myGrid.RowDefinitions.Add(rowDef);
        }

        // define the columns
        // column for Line
        ColumnDefinition colDef1 = new ColumnDefinition();
        colDef1.Width = new GridLength(8, GridUnitType.Star);
        myGrid.ColumnDefinitions.Add(colDef1);
        // column for TextBlock
        ColumnDefinition colDef2 = new ColumnDefinition();
        colDef2.Width = new GridLength(1, GridUnitType.Star);
        myGrid.ColumnDefinitions.Add(colDef2);
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        for (int i = 0; i < 5; i++)
        {
            // draw dashed line
            Line line = new Line()
            {
                X2 = myGrid.ColumnDefinitions[0].ActualWidth, // actual width is 0
                Stroke = Brushes.Black,
                StrokeDashArray = { 10, 10, 10, 10 },
                StrokeThickness = 1
            };
            Grid.SetRow(line, i);
            Grid.SetColumn(line, 0);
            myGrid.Children.Add(line);
            // draw text
            Viewbox vb_txtBlock = new Viewbox();
            TextBlock txtBlock = new TextBlock();
            txtBlock.Text = "Line " + (i + 1);
            vb_txtBlock.Child = txtBlock;
            Grid.SetRow(vb_txtBlock, i);
            Grid.SetColumn(vb_txtBlock, 1);
            myGrid.Children.Add(vb_txtBlock);
        }
    }

ActualWidth调试时我得到一个有效的。

于 2014-12-19T11:19:43.207 回答
1

您可以在覆盖的方法OnRenderSizeChanged()中添加代码,如下所示

    protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
    {
        base.OnRenderSizeChanged(sizeInfo);
        ////Here put your code.
    }
于 2014-12-19T11:32:15.740 回答