wpf - 测量实际宽度和实际高度

时间:2014-12-19 11:07:16

标签: c# wpf grid actualwidth actualheight

我在XAML中定义了一个Grid,比如

<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#代码中,我可以测量实际的宽度和高度,以便我可以使用实际宽度和高度?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

您可以在重写方法OnRenderSizeChanged()中添加代码,如下所示

    protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
    {
        base.OnRenderSizeChanged(sizeInfo);
        ////Here put your code.
    }