英語の文法が苦手で申し訳ありません。WPF グリッドに四角形を描画しようとしています。私はそれを描きますが、この長方形はグリッドの右側と下側に表示されません。私は何をすべきか?これは私の XAML とコードです。
< ウィンドウ x:Class="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);
}
}