C# wpf网格中的缩放线

C# wpf网格中的缩放线,c#,wpf,C#,Wpf,我有用户控制: <UserControl x:Class="WpfExt.ColorLine.TimeLineControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schema

我有用户控制:

<UserControl x:Class="WpfExt.ColorLine.TimeLineControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" 
             d:DesignHeight="40" d:DesignWidth="400">
    <Grid x:Name="DynamicGrid">

    </Grid>
</UserControl>
在代码隐藏中:

public readonly static DependencyProperty TotalHoursProperty = DependencyProperty.Register("TotalHours",
            typeof(int),
            typeof(TimeLineControl),
            new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsParentMeasure, TotalHoursChangedCallback));


        private static void TotalHoursChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            ((TimeLineControl)dependencyObject).ChangeTimeLineItems((int)e.NewValue);
        }

        public int TotalHours
        {
            get { return (int)GetValue(TotalHoursProperty); }
            set { SetValue(TotalHoursProperty, value); }
        }

        private void ChangeTimeLineItems(int totalHours)
        {
            if (totalHours < 1 || totalHours > 24) throw new InvalidOperationException("TotalHours should be in 1..24 range.");

            DynamicGrid.Children.Clear();

            AddExtremeLine(0, HorizontalAlignment.Left);

            for (var index = 1; index < totalHours * 2 - 1; index += 2)
            {
                AddLine(index, 30);
                AddLine(index + 1, 40);
            }
            AddLine(totalHours * 2 - 1, 30);

            AddExtremeLine(totalHours * 2, HorizontalAlignment.Right);
        }

        private void AddExtremeLine(int column, HorizontalAlignment horizontalAlignment)
        {
            var columnDefinition = new ColumnDefinition();
            var gridLength = new GridLength(0.5, GridUnitType.Star);
            columnDefinition.Width = gridLength;

            DynamicGrid.ColumnDefinitions.Add(columnDefinition);

            var line = new Line
            {
                X1 = 0,
                X2 = 0,
                Y1 = 20,
                Y2 = 40,
                Stroke = Brushes.Black,
                StrokeThickness = 1,
                HorizontalAlignment = horizontalAlignment
            };
            Grid.SetColumn(line, column);

            DynamicGrid.Children.Add(line);
        }

        private void AddLine(int column, double y2)
        {
            var columnDefinition = new ColumnDefinition();
            var gridLength = new GridLength(1, GridUnitType.Star);
            columnDefinition.Width = gridLength;

            DynamicGrid.ColumnDefinitions.Add(columnDefinition);

            var line = new Line
            {
                X1 = 0,
                X2 = 0,
                Y1 = 20,
                Y2 = y2,
                Stroke = Brushes.Black,
                StrokeThickness = 1,
                HorizontalAlignment = HorizontalAlignment.Center
            };
            Grid.SetColumn(line, column);

            DynamicGrid.Children.Add(line);
        }
所以如果我像这样使用它:

<colorLine:TimeLineControl Height="100" TotalHours="6" />
它看起来像:

所以问题是:
如何更改代码,使线条缩放以填充控件高度,但在宽度更改时保持现有行为只有它们之间的空间会缩放,而不是线条。

将Y1、Y2的设置更改为基于RenderSize.height的设置。i、 e.类似于:

Y1 = Grid.RenderHeight - 40;
Y2 = Grid.RenderHeight - 20;

显然,20和40开关是您现在“测量”要留下的空白量,而不是线的长度。

如果它只是关于一个以所示方式绘制记号的控件,我想我会做得不同,并创建一个非常小的自定义控件。这就是你所需要的,很明显它是如何工作的。当然,您可以根据自己的喜好添加依赖项属性。仅通过使用这种方式,它就填满了所有给定的空间

public class HourTicks : FrameworkElement
{
    static HourTicks ()
    {
    }

    protected override void OnRender (DrawingContext drawingContext)
    {
        int hours = 10;
        Pen pen  = new Pen(Brushes.Black, 1);

        drawingContext.DrawLine (pen, new Point (0, 0), new Point (0, ActualHeight));
        drawingContext.DrawLine (pen, new Point (ActualWidth, 0), new Point (ActualWidth, ActualHeight));

        int numTicks = hours * 2;
        double delta = ActualWidth / numTicks;
        double x = delta;

        for (int i = 0; i <numTicks; i++)
        {
            drawingContext.DrawLine (pen, new Point (x, 0), new Point (x, ActualHeight / 3));
            x += delta;
            drawingContext.DrawLine (pen, new Point (x, 0), new Point (x, ActualHeight / 2));
            x += delta;
        }
    }
}

似乎在我的dependency属性赋值和回调事件触发时,没有实际的高度、渲染大小等。