表示する座標は、Canvas.SetTop、Canvas.SetLeft で指定します。
MainWindow.xaml
<Window x:Class="WpfApplication4.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="200" Width="200">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Button Click="addButton_Click">Add</Button>
<Button Click="removeButton_Click">Remove</Button>
</StackPanel>
<Canvas Grid.Row="1" Name="baseCanvas">
<Canvas Name="innerCanvas1" Height="30" Width="40" Top="20" Left="20" Background="Black">
<TextBlock Foreground="White">装飾</TextBlock>
</Canvas>
</Canvas>
</Grid>
</Window>
MainWindow.xaml.cs
using System.Windows;using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace WpfApplication4
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void addButton_Click(object sender, RoutedEventArgs e)
{
if (this.baseCanvas.Children.Count == 1)
{
Rectangle rect = new Rectangle();
rect.Width = this.innerCanvas1.DesiredSize.Width;
rect.Height = this.innerCanvas1.DesiredSize.Height;
Canvas.SetTop(rect, 50);
Canvas.SetLeft(rect, 100);
VisualBrush brush = new VisualBrush(this.innerCanvas1);
brush.Opacity = 0.3;
rect.Fill = brush;
this.baseCanvas.Children.Add(rect);
}
}
private void removeButton_Click(object sender, RoutedEventArgs e)
{
if (this.baseCanvas.Children.Count > 1)
{
this.baseCanvas.Children.RemoveAt(1);
}
}
}
}
0 件のコメント:
コメントを投稿