実現したいこと
ユーザーコントロール上にあるcanvasをクリアしたい。
前提
canvasがあるユーザーコントロールがウィンドウ上にあるのですが、
ウィンドウ側からcanvasをクリアする方法がわかりません。
該当のソースコード
XAML
1<!-- MainWindow.xaml --> 2<Window x:Class="WpfApp7.Views.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 7 xmlns:local="clr-namespace:WpfApp7.Views" 8 mc:Ignorable="d" 9 Title="MainWindow" Height="450" Width="800"> 10 <Grid> 11 <StackPanel> 12 <local:UserControl1 13 x:Name="uc1" 14 Background="Blue"/> 15 <Button Content="ボタン1" 16 Command="{Binding ButtonCommand}"/> 17 </StackPanel> 18 </Grid> 19</Window>
C#
1// MainWindow.xaml.cs2using System.Windows;3using WpfApp7.ViewModels;4 5namespace WpfApp7.Views 6{7 /// <summary>8 /// MainWindow.xaml の相互作用ロジック9 /// </summary>10 public partial class MainWindow : Window 11 {12 public MainWindow()13 {14 InitializeComponent();15 DataContext = new MainWindowViewModel();16 }17 }18}
XAML
1<!-- UserControl1.xaml --> 2<UserControl x:Class="WpfApp7.Views.UserControl1" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 7 xmlns:local="clr-namespace:WpfApp7.Views" 8 mc:Ignorable="d" 9 d:DesignHeight="450" d:DesignWidth="800"> 10 <Grid> 11 <Canvas x:Name="canvas1"/> 12 </Grid> 13</UserControl>
C#
1// UserControl1.xaml.cs2using System.Windows.Controls;3using System.Windows.Media;4using System.Windows.Shapes;5 6namespace WpfApp7.Views 7{8 /// <summary>9 /// UserControl1.xaml の相互作用ロジック10 /// </summary>11 public partial class UserControl1 : UserControl 12 {13 public UserControl1()14 {15 InitializeComponent();16 17 Rectangle rect = new Rectangle();18 rect.Fill = new SolidColorBrush(Colors.Red);19 Opacity = 0.7;20 Canvas.SetLeft(rect, 0);21 Canvas.SetTop(rect, 0);22 rect.Width = 100;23 rect.Height = 100;24 25 canvas1.Children.Add(rect);26 }27 28 public void Clear()29 {30 canvas1.Children.Clear();31 }32 }33}34
C#
1// MainWindowViewModel.cs2namespace WpfApp7.ViewModels 3{4 class MainWindowViewModel : INotifyPropertyChanged 5 {6 /// <summary>7 /// プロパティ変更通知イベント8 /// </summary>9 public event PropertyChangedEventHandler PropertyChanged;10 11 /// <summary>12 /// プロパティ変更通知イベント呼び出しメソッド13 /// </summary>14 /// <param name="propertyName">変更対象プロパティ名を表す文字列</param>15 protected void RaisePropertyChanged([CallerMemberName]string propertyName = null)16 => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));17 18 private DelegateCommand _buttonCommand;19 /// <summary>20 /// ボタン動作21 /// /summary>22 public DelegateCommand ButtonCommand 23 {24 get 25 {26 if (_buttonCommand == null)27 {28 _buttonCommand = new DelegateCommand(_ =>29 {30 //ここでcanvasをクリアしたい31 Console.WriteLine("ボタンが押された");32 });33 }34 return _buttonCommand;35 }36 }37 }38}39
試したこと
自分なりに調べたのですが、DependencyPropertyを使用するのかと思い、いろいろ探してみたのですが、変数のバインディングしか見つけることができませんでした。
予想ですが、ユーザーコントロール内にクリア関数を作成し、ウィンドウ側から呼び出す方法をとると思うのですが、方法がわかりません。
大変恐縮ですが、ご教授いただけたら幸いです。
補足情報(FW/ツールのバージョンなど)
visual studio 2017
.net framework 4.7.2

0 コメント