我對我的英語語法弱點表示歉意。我嘗試在WPF網格中繪製一個矩形。我畫了它,但是這個矩形在網格的右側和底部都沒有看到。我該怎麼辦?這是我的XAML和代碼。繪製的矩形未出現在wpf網格
< 窗口x:類= 「DrawCircle.MainWindow」 背後的代碼
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
MouseLeftButtonUp="window_MousLefteUp" MouseRightButtonUp="window_MouseRightButtonUp"
MouseMove="Grid_MouseMove"
MouseDown="Grid_MouseDown" >
<Grid x:Name="window" Margin="0,0,0,0" Background="PaleGreen" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ClipToBounds="False">
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform x:Name="scaleTransform1" ScaleX="0.5" ScaleY="0.5" />
<TranslateTransform x:Name="translateTransform1" X="0" Y="0" />
</TransformGroup>
</Grid.RenderTransform>
</Grid>
它是:
public partial class MainWindow : Window
{
bool cornerPointed = false;
Point corner1;
Rectangle myRectangle;
public MainWindow()
{
InitializeComponent();
myRectangle = new Rectangle();
myRectangle.HorizontalAlignment = HorizontalAlignment.Left;
myRectangle.VerticalAlignment = VerticalAlignment.Top;
myRectangle.Stroke = Brushes.Red;
window.Children.Add(myRectangle);
}
private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
{
cornerPointed = true;
corner1 = new Point(e.GetPosition(this.window).X, e.GetPosition(this.window).Y);
myRectangle.Margin = new Thickness(corner1.X, corner1.Y, 0, 0);
}
private void Grid_MouseMove(object sender, MouseEventArgs e)
{
if (cornerPointed == true)
{
Point corner2 = e.GetPosition(this.window);
myRectangle.Margin = new Thickness(System.Math.Min(corner1.X, corner2.X),
System.Math.Min(corner1.Y, corner2.Y),
0, 0);
myRectangle.Width = Math.Abs(e.GetPosition(this.window).X - corner1.X);
myRectangle.Height = Math.Abs(e.GetPosition(this.window).Y - corner1.Y);
}
}
private void window_MousLefteUp(object sender, MouseButtonEventArgs e)
{
cornerPointed = false;
myRectangle = new Rectangle();
myRectangle.HorizontalAlignment = HorizontalAlignment.Left;
myRectangle.VerticalAlignment = VerticalAlignment.Top;
myRectangle.Stroke = Brushes.Red;
window.Children.Add(myRectangle);
}
}
它的硬頂告訴你這個職位要求什麼,但上運行你的例子,我可以看到紅色矩形得到正確繪製的網格內,直到你遇到電網邊緣,其中右側和底部兩側唐不會畫出來,這是你想要阻止的嗎? – Andy