如何获得未渲染元素的预期宽度和高度?

所以我有一个画布,要在其中绘制16条垂直线。画布还位于名为TimeLine的用户控件元素的顶部。如果我在XAML中手动创建线,则它们在用户控件的顶部是可见的,所以这不是问题。这些行以编程方式创建,并存储在绑定到ItemsControl的ObservableCollection中。因为我希望所有行之间都具有相同的间距(我特别希望16行),所以我使用画布的 actualWidth 属性。但是,似乎我的行创建方法在渲染画布之前运行,因此actualWidth返回null(我认为,在调试时,它只是说不是数字),因此我的行的长度为0,并且不可见。有什么方法可以等待画布渲染或在尺寸更改时得到通知吗?我不想使用特定的像素数量,因为我需要一个自适应的布局。

我尝试将 DrawTimeLineGridLines()设置为页面的 Loaded 事件的事件处理程序,但这没有任何作用。

XAML

<local:TimeLine Grid.Row="1" Background="LightGray" x:Name="timeLine" Grid.ColumnSpan="5" />

<ItemsControl x:Name="gridCanvas" Grid.Row="1" ItemsSource="{Binding TimeLineGridLines}">
    <ItemsControl.ItemsPanel>
        <itemspaneltemplate>
            <Canvas/>
        </itemspaneltemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Line X1="{Binding From.X}" Y1="{Binding From.Y}" X2="{Binding To.X}" Y2="{Binding To.Y}" Stroke="DarkBlue" StrokeThickness="3" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

MyCustomLine

class MyCustomLine
    {
        public Point From { get; set; }
        public Point To { get; set; }
        public Brush Brush { get; set; }
        public double StrokeThickness { get; set; }

    }    

视图模型

private ObservableCollection<MyCustomLine> gridLines = new ObservableCollection<MyCustomLine>();

public ObservableCollection<MyCustomLine> TimeLineGridLines
        {
            get { return gridLines; }
            set
            {
                if (gridLines != value)
                {
                    gridLines = value;
                    OnPropertyChange();
                }
            }
        }

XAML.cs

private void DrawTimeLineGridLines()
        {
            var gridLines = new ObservableCollection<MyCustomLine>();
            double horizontalSpacing = gridCanvas.Width / 15;

            for (int i = 0; i <= 15; i++)
            {
                var line = new MyCustomLine()
                {
                    From = new Point(horizontalSpacing * i,0),To = new Point(horizontalSpacing * i,gridCanvas.Height),Brush = brushes.DarkBlue,StrokeThickness = 5,};

                gridLines.Add(line);
            }

            viewmodel.TimeLineGridLines = gridLines;
        }

所需结果是在画布上绘制垂直线。

as120n 回答:如何获得未渲染元素的预期宽度和高度?

以下XAML绘制了一组垂直线,而无需进行任何人工尺寸或位置计算:

<ItemsControl ItemsSource="{Binding}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Rows="1"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Rectangle Width="3" Fill="DarkBlue"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

示例DataContext:

DataContext = Enumerable.Range(0,16);

您可能想要用n线绘制n-1项,以便在左右边缘获得均匀间隔的间隙。为此,请使Rectangle左对齐,不要绘制第一个矩形:

<ItemsControl ItemsSource="{Binding}" AlternationCount="1000000">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Rows="1"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Visibility" Value="Hidden"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Rectangle Width="3" Fill="DarkBlue"
                       HorizontalAlignment="Left" Margin="-1.5,0"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
本文链接:https://www.f2er.com/3116740.html

大家都在问