canvas上のクリックした場所に図形やフォントを配置するというプログラムを、WPFで作っています。言語はC#、開発環境はvisual studio 2012 for Desktopを使用しています。
子要素をクリックで選んで消去させたいのですが、どのようにすれば良いのか分からないため、教えて頂きたいです。
###該当のソースコード
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication1 { /// <summary> /// MainWindow.xaml の相互作用ロジック /// </summary> public partial class MainWindow : Window { double x = 0; double y = 0; double x1 = 0; double x2 = 0; double y1 = 0; double y2 = 0; double x3 = 0; double y3 = 0; bool flag = true; bool flag1 = true; int counter = 0; public MainWindow() { InitializeComponent(); } // 線描画モード private void button_Click(object sender, RoutedEventArgs e) { flag = false; } // マウスクリック位置(押したとき)取得 private void Root_MouseDown(object sender, MouseButtonEventArgs e) { if (flag == false) { Point pos1 = e.GetPosition(Root); x1 = pos1.X; y1 = pos1.Y; } if (flag1 == false) { Point pos3 = e.GetPosition(Root); x3 = pos3.X; y3 = pos3.Y; textblock(); } } // マウスクリック位置(離したとき)取得 private void Root_MouseUp(object sender, MouseButtonEventArgs e) { if (flag == false) { Point pos2 = e.GetPosition(Root); x2 = pos2.X; y2 = pos2.Y; x = x2; y = y2; CreateLine(); } } // 線描画 public void CreateLine() { Line blackLine = new Line(); blackLine.X1 = x1; blackLine.Y1 = y1; blackLine.X2 = x2; blackLine.Y2 = y2; SolidColorBrush blackBrush = new SolidColorBrush(); blackBrush.Color = Colors.Black; blackLine.StrokeThickness = 1; blackLine.Stroke = blackBrush; canvas.Children.Add(blackLine); } // 文字描画モード private void button1_Click(object sender, RoutedEventArgs e) { flag = true; flag1 = false; } // textboxをtextblockにバインド public void textblock() { TextBlock tblock = new TextBlock(); tblock.Width = 23; tblock.MouseEnter += tblock_MouseEnter; tblock.MouseLeave += tblock_MouseLeave; tblock.SetValue(Canvas.TopProperty, y3); tblock.SetValue(Canvas.LeftProperty, x3); Binding bind = new Binding(); bind.ElementName = "textBox1"; bind.Path = new PropertyPath(TextBox.TextProperty); bind.Mode = BindingMode.OneTime; tblock.SetBinding(TextBlock.TextProperty, bind); canvas.Children.Add(tblock); counter++; flag1 = true; } private void tblock_MouseLeave(object sender, MouseEventArgs e) { TextBlock tb = sender as TextBlock; tb.Background = SystemColors.WindowBrush; } void tblock_MouseEnter(object sender, MouseEventArgs e) { TextBlock tb = sender as TextBlock; tb.Background = SystemColors.HighlightBrush; } } }
xmal
<Window x:Class="WpfApplication1.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"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="0*"/> <ColumnDefinition Width="100px"/> </Grid.ColumnDefinitions> <Viewbox x:Name="View" Stretch="UniformToFill"> <Border x:Name="Root" Background="Transparent" MouseDown="Root_MouseDown" MouseUp="Root_MouseUp" Width="300" Height="400"> <Canvas x:Name="canvas" Background="Transparent" /> </Border> </Viewbox> <StackPanel Orientation="Vertical" Background="Gray" Grid.Column="2"> <Button x:Name="button" Content="線" Click="button_Click"/> <TextBox x:Name="textBox1" HorizontalAlignment="Left" Width="100"/> <Button x:Name="button1" Height="24" Click="button1_Click" VerticalAlignment="Top" HorizontalAlignment="Left" Content="文字" Width="100"/> </StackPanel> </Grid> </Window>
上記のソースでは、ボタンクリックで追加する子要素を選択し、canvasをクリックして貼り付けることまでのみ可能です。
宜しくお願いします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/27 22:32