【问题标题】:C# WPF drawing gridC# WPF 绘图网格
【发布时间】:2020-08-19 19:47:09
【问题描述】:

我想做一个这样的绘图网格:

谁能帮帮我? 我现在有这个:

        <Canvas>
            <Canvas.Background>
                <DrawingBrush TileMode="Tile" Viewport="0 0 40 40" ViewportUnits="Absolute" Opacity="0.5">
                    <DrawingBrush.Drawing>
                        <GeometryDrawing>
                            <GeometryDrawing.Geometry>
                                <RectangleGeometry Rect="0,0,50,50"/>
                            </GeometryDrawing.Geometry>
                            <GeometryDrawing.Pen>
                                <Pen Brush="#FF323232" Thickness="0.25"/>
                            </GeometryDrawing.Pen>
                        </GeometryDrawing>
                    </DrawingBrush.Drawing>
                </DrawingBrush>
            </Canvas.Background>
        </Canvas>

【问题讨论】:

    标签: c# wpf canvas


    【解决方案1】:

    我找到了使用两个Canvases 的方法。但我已将RectangleGeometry 替换为PathGeometry 以使线条更清晰。

    <Grid Background="#102035">
        <Grid.Resources>
            <Pen x:Key="GrayPenKey" Brush="Gray"/>
            <GeometryDrawing x:Key="SmallGridDrawing" Pen="{StaticResource GrayPenKey}" Geometry="M 0 0 L 40 0 L 40 40"/>
            <GeometryDrawing x:Key="LargeGridDrawing" Pen="{StaticResource GrayPenKey}" Geometry="M 0 0 L 200 0 L 200 200"/>
            <DrawingBrush x:Key="SmallGridBrush" TileMode="Tile" Viewport="0 0 40 40" ViewportUnits="Absolute" Opacity="0.5" Drawing="{StaticResource SmallGridDrawing}" />
            <DrawingBrush x:Key="LargeGridBrush" TileMode="Tile" Viewport="40 40 200 200" ViewportUnits="Absolute" Drawing="{StaticResource LargeGridDrawing}"/>
        </Grid.Resources>
        <Canvas Background="{StaticResource SmallGridBrush}"/>
        <Canvas Background="{StaticResource LargeGridBrush}">
            <Line X1="40" Y1="0" X2="40" Y2="440" Stroke="Green"/>
            <Line X1="40" Y1="440" X2="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType=Canvas}}" Y2="440" Stroke="Red"/>
        </Canvas>
    </Grid>
    

    【讨论】:

    • 谢谢!这是一个很好的解决方案。唯一的缺点是它是由 2 个画布构建的。
    • @A.N.我试图用一个单独的来构建它并成功但没有达到线条的清晰度。所以,以上是我所有视觉效果中最好的。
    【解决方案2】:

    这是你需要的吗?

    XAML:

    <Canvas x:Name="GridCanvas" Width="400" Height="400"/>
    

    C#: 初始化组件();

    GridCanvas.Background = Brushes.DarkBlue;
    
    int w = 400;
    int h = 400;
    
    for (int c = 0; c < w; c += 10)
    {
        Line line = new Line
        {
            X1 = c,
            X2 = c,
            Y1 = 0,
            Y2 = h,
            StrokeThickness = 1
        };
    
        if (c % 100 == 0)
            line.Stroke = new SolidColorBrush(Color.FromRgb(255, 255, 255));
        else
            line.Stroke = new SolidColorBrush(Color.FromArgb(160, 255, 255, 255));
    
        GridCanvas.Children.Add(line);
    }
    
    
    for (int r = 0; r < h; r += 10)
    {
        Line line = new Line
        {
            X1 = 0,
            X2 = w,
            Y1 = r,
            Y2 = r,
            StrokeThickness = 1
        };
    
        if (r % 100 == 0)
            line.Stroke = new SolidColorBrush(Color.FromRgb(255, 255, 255));
        else
            line.Stroke = new SolidColorBrush(Color.FromArgb(160, 255, 255, 255));
    
        GridCanvas.Children.Add(line);
    }
    

    【讨论】:

    • 感谢大隈的回答!这是一种干净的方式 :) MaxWidth 和 MaxHeight 屏幕可以这样做吗?
    • 如果 WindowState == Normal,您可以使用 window.(ActualWidth/ActualHeight)。和 System.Windows.SystemParameters.(PrimaryScreenWidth/PrimaryScreenHeight) 如果 WindowState == Maximized
    猜你喜欢
    • 2018-06-24
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    相关资源
    最近更新 更多