简体   繁体   中英

Drawing a stretchable Line in WPF

I need to draw a line which stretch when its parent container stretch itself and it collapse when its parents collapse itself.

I develop a user control for this purpose and this control is placed in scroll viewer for some reasons.

I have binded the X2 Property of the line with the width of parent control. It solves the problem while stretching the parent control but while collapsing it is not letting the control decrease its size and scrollviewer adds a scrollbar for it.

 Binding binding = new Binding("ActualWidth")
        {
            ElementName = Grid1.Name
        };
        Line mainLine = new Line
        {
            X1 = 0,
            HorizontalAlignment=System.Windows.HorizontalAlignment.Stretch,
            StrokeThickness = 2,              
            VerticalAlignment = VerticalAlignment.Center
        };

        BindingOperations.SetBinding(mainLine, Line.X2Property, binding);
        Grid1.Children.Add(mainLine);
        Grid.SetColumnSpan(mainLine, 4);

Try using a Rectangle to do that:

<Grid>
    <Rectangle HorizontalAlignment="Stretch" 
               VerticalAlignment="Center" 
               Fill="Black" 
               Height="2"/>
</Grid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM