我的窗口有超过50个按钮,每个按钮都有一个画布。
按钮1代表:
<Button x:Name="button1" HorizontalAlignment="Left" Margin="98,292,0,0" VerticalAlignment="Top" Width="85" Height="115" FontSize="25" Background="Red" BorderBrush="White" Foreground="White" Click="button1_Click" MouseDown="button1_MouseDown">
<StackPanel>
<TextBlock TextAlignment="Center" RenderTransformOrigin="-0.643,1.424" HorizontalAlignment="Center" VerticalAlignment="Center"><Run Text="1"/></TextBlock>
<Canvas x:Name="canvas1"/>
</StackPanel>
</Button>
所以我的问题是,如何在button1_Click事件中获取我的画布名称? 我想创建一个函数,所以我可以触发不同的画布'。 例如:
void clearCanvas(int canvasNumber)
{
canvas(+canvasNumber).Clear();
}
答案 0 :(得分:1)
我想你想要这样的东西:
private void button1_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
Canvas canvas = ((StackPanel)btn.Content).Children
.OfType<Canvas>()
.First();
canvas.Children.Clear();
//at this point you can also get corresponding `Canvas` name
//(if you really have to) :
//String name = canvas.Name;
}