当您双击屏幕所在的位置时,我有一些要使其“出现”的矩形:

<StackPanel>
    <Rectangle Name="MyRect1" Opacity="0" Height="100" DoubleTapped="MyRect1_DoubleTapped" Fill="Aqua" />
    <Rectangle Name="MyRect2" Opacity="0" Height="100" DoubleTapped="MyRect2_DoubleTapped" Fill="Black"/>
    <Rectangle Name="MyRect3" Opacity="0" Height="100" DoubleTapped="MyRect3_DoubleTapped" Fill="Blue"/>
</StackPanel>


我当前的背后代码-可以工作:

private void MyRect1_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
    MyRect1.Opacity = 1;
}

private void MyRect2_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
    MyRect2.Opacity = 1;
}

private void MyRect3_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
    MyRect3.Opacity = 1;
}


由于我可能想要一打或更多这些出现的矩形,是否有什么方法可以对所有这些矩形使用一个事件处理程序?

即不是每个矩形都有处理程序,而是类似:

<StackPanel>
    <Rectangle Name="MyRect1" Opacity="0" Height="100" DoubleTapped="MyRect_DoubleTapped" Fill="Aqua" />
    <Rectangle Name="MyRect2" Opacity="0" Height="100" DoubleTapped="MyRect_DoubleTapped" Fill="Black"/>
    <Rectangle Name="MyRect3" Opacity="0" Height="100" DoubleTapped="MyRect_DoubleTapped" Fill="Blue"/>
</StackPanel>


使用单个处理程序MyRect_DoubleTapped

private void MyRect_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
    (some way to tell which rectangle was DoubleTapped)
    (the identified rectangle).Opacity = 1;
}

最佳答案

您可以使用sender来了解被窃听的是哪一个

private void MyRect_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
    Rectangle myRect = (Rectangle)sender;
    myRect.Opacity = 1;
}


sender是引发事件的对象。

关于c# - 可重用的代码,使双击时矩形的不透明度为零,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43454710/

10-16 23:05