【问题标题】:Line overhang on canvas size画布尺寸上的线悬垂
【发布时间】:2020-04-19 14:09:27
【问题描述】:

我想在我的 WPF 应用程序的画布上画一条线。我的问题,如果我在画布上添加突出的线坐标,我也可以看到这部分线。

我附上了一张图片。:

黄色框是我的画布区域。

<Window x:Name="MainWindow01" x:Class="_001Linewpf.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:_001Linewpf"
    mc:Ignorable="d"
    Title="Scaleit" Height="874" Width="1024" Loaded="MainWindow01_Loaded">
<Grid ShowGridLines="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="80"></ColumnDefinition>
        <ColumnDefinition Width="*"></ColumnDefinition>
        <ColumnDefinition Width="250"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="80"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
        <RowDefinition Height="80"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid Grid.Row="0" Grid.ColumnSpan="3" HorizontalAlignment="Center" VerticalAlignment="Center"></Grid>
    <Grid Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center"></Grid>
    <Grid Name="drawgrid" Background="Yellow" Grid.Row="1" Grid.Column="1">
        <Border x:Name="CanvasBorder" BorderThickness="5" BorderBrush="Black">
            <Canvas x:Name="Canvas01">
                <Canvas.Background>
                    <SolidColorBrush Color="Black" Opacity="0.15"/>
                </Canvas.Background>
            </Canvas>
        </Border>
    </Grid>

    <Grid Grid.Row="1" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center"></Grid>
    <Grid Grid.Row="2" Grid.ColumnSpan="3" HorizontalAlignment="Center" VerticalAlignment="Center"></Grid>
    <Label x:Name="Label01" Content="Label" Grid.Column="2" HorizontalAlignment="Left" Margin="152,49,0,0" Grid.Row="1" VerticalAlignment="Top"/>
    <Label x:Name="Label02" Content="Label" Grid.Column="2" HorizontalAlignment="Left" Margin="152,139,0,0" Grid.Row="1" VerticalAlignment="Top"/>
</Grid>

这是我的 c#

    private void MainWindow01_Loaded(object sender, RoutedEventArgs e)
    {
        Line myLine = new Line();

        myLine.Stroke = System.Windows.Media.Brushes.Black;

        myLine.X1 = 100;
        myLine.X2 = 600;  // 150 too far
        myLine.Y1 = 100;
        myLine.Y2 = -200;

        myLine.StrokeThickness = 1;

        Canvas01.Children.Add(myLine);

        //Label01.Content = "x: " + drawgrid.Point;
        //Label02.Content = "y: " + drawgrid.Height.ToString();
    }
}

}

我只想绘制画布区域。就像一个 cad 程序。表单的其他部分将是按钮等。

【问题讨论】:

  • Here 是一个类似的问题。使用这个ClipToBounds="True"
  • @Andrea 您可以通过简单的 xaml 示例将此添加为答案

标签: c# wpf canvas


【解决方案1】:

您可以将ClipToBounds 设置为true:

<Canvas x:Name="Canvas01" ClipToBounds="True">
    <Canvas.Background>
        <SolidColorBrush Color="Black" Opacity="0.15"/>
    </Canvas.Background>
</Canvas>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多