0

我有一个在画布上画线的方法,如下所示:

public void drawoncanvas(double[] coord, string fullkey)
{
    myLine = new Line();
    myLine.X1 = coord[0];
    myLine.X2 = coord[2];
    myLine.Y1 = coord[1];
    myLine.Y2 = coord[3];

    MainWindow.mycanvas.Children.Add(myLine);

    myLine.MouseEnter += mymouse;    //add MousEnter Event 

    myLine.MouseLeave += mymouseleave; //add MouseLeave Event  
}

MouseEnterEvent 然后在触发事件的行中添加一个DropShadowEffect

public void mymouse(object sender, MouseEventArgs e)
{
    Line thisline = sender as Line;

    thisline.Effect =  new DropShadowEffect{Color = new Color { A = 255, R = 255, G = 255, B = 0 }, Direction = 320,ShadowDepth = 0, Opacity = 1};

}

这工作正常。但是现在我想在鼠标光标不再在线时删除此效果:

public void mymouseleave(object sender, MouseEventArgs e)
{
    Line thisline = sender as Line;

    thisline.Effect = null;  //this doesn't do anything     

}

我知道这可能是一个非常简单的问题,但到目前为止我找不到任何解决方案。

编辑:

好的,我找到了导致问题的原因,但仍然不知道解决方案:我不仅在 MouseEnter 事件中创建阴影,而且还显示一个提示,所以我的完整代码实际上是:

   public void drawoncanvas(double[] coord, string fullkey)
    {

    myLine = new Line();
    myLine.Stroke = Brushes.Red;
    myLine.X1 = coord[0];
    myLine.X2 = coord[2];
    myLine.Y1 = coord[1];
    myLine.Y2 = coord[3];
    myLine.Tag = fullkey;                                       

    MainWindow.mycanvas.Children.Add(myLine);

    myLine.MouseEnter += mymouse;     

    myLine.MouseLeave += mymouseleave;   
}


ToolTip tt = new ToolTip();

public void mymouse(object sender, MouseEventArgs e)
{

    Line thisline = sender as Line;

    string data = Convert.ToString(thisline.Tag);

    string[] splitdata = data.Split('/');                  

    tt.Content = String.Concat(splitdata[0], " -> ", splitdata[3] ,"kt -> ", splitdata[1]);
    tt.StaysOpen = false;
    tt.IsOpen = true;           //the removing of the shadow on MouseLeave works when I comment this line out

    thisline.Effect =  new DropShadowEffect{Color = new Color { A = 255, R = 255, G = 255, B = 0 }, Direction = 320,ShadowDepth = 0, Opacity = 1};

}

public void mymouseleave(object sender, MouseEventArgs e)
{
    Line thisline = sender as Line;

    tt.IsOpen = false;

    thisline.Effect = null;

}

如果我只是注释掉tt.IsOpen = true;并且不显示工具提示,那么一切正常......我真的很困惑。

编辑2:

这里是 XAML:

<Window x:Class="mapO1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:mapO1"
        Title="mapO1" Height="600" Width="750" WindowStartupLocation="CenterScreen" SizeChanged="MainWindow_SizeChanged">

    <Grid>
        <Grid x:Name="mygrid">


            <local:ZoomBorder x:Name="border" ClipToBounds="True" Background="Black">

                <Grid Name="maingrid">
                    <Image Name="image1" />
                    <Canvas Name="mycanvas" HorizontalAlignment="Left" VerticalAlignment="Top" ClipToBounds="False"/>
                </Grid>

            </local:ZoomBorder>

            <Button x:Name="button1" Content="Fullscreen on/off" HorizontalAlignment="Right" Height="18" Margin="0,20,50,0" VerticalAlignment="Top" Opacity="0.5" Width="108" FontWeight="Bold" Click="button1_Click"/>

            <ComboBox x:Name="comboBox1" Height="24" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Margin="10,20,0,0" Opacity="0.5" FontWeight="Bold" Text="Select Period" SelectionChanged="comboBox1_SelectionChanged"/>

            <ComboBox x:Name="comboBox2" Height="24" HorizontalAlignment="Left" VerticalAlignment="Top" Width="150" Margin="10,60,0,0" FontWeight="Bold" Opacity="0.5" Text="Select Stream" DropDownClosed="comboBox2_DropDownClosed"/>


        </Grid>
    </Grid>

</Window>
4

0 回答 0