Fork me on GitHub

ToolTip

 

本例中有三个控件,两个TextBox和一个Ellipse。第一个TextBox中的ToolTipService.InitialShowDelay属性是ToolTip延迟显示的时间,ToolTipService.ShowDuration属性是ToolTip显示的时间,ToolTipService.BetweenShowDelay属性是当鼠标从一个控件移动到另一个控件上时的时间如果少于它的值,第二个控件的ToolTip就会立刻显示,如果大于这个值,就会按照第二个控件ToolTipService.InitialShowDelay的值来延迟显示。

XAML代码:

<Grid x:Name="LayoutRoot">

<TextBox HorizontalAlignment="Left" ToolTipService.InitialShowDelay="1000"                ToolTipService.ShowDuration="5000" ToolTipService.BetweenShowDelay="2000" ToolTip="Useful information goes here."  VerticalAlignment="Top"     Height="125" Text="ToolTip with delay" Width="107.673"/>

   <TextBox HorizontalAlignment="Left" Margin="111.673,0,0,0" VerticalAlignment="Top" Width="171.687" Height="125" Text="ToolTip with non-text content">

     <TextBox.ToolTip>

            <ToolTip>

           <DockPanel Width="50" Height="40">

             <Image Source="data\flower.jpg"/>

             <TextBlock>Useful information goes here.</TextBlock>

           </DockPanel>

        </ToolTip>

     </TextBox.ToolTip>

</TextBox>

   <Ellipse Width="75"

         Fill="Gray"

         HorizontalAlignment="Left"

         ToolTipService.InitialShowDelay="1000"

         ToolTipService.ShowDuration="7000"

         ToolTipService.BetweenShowDelay="2000" Margin="0,143.5,0,0" VerticalAlignment="Top" Height="77">

 <Ellipse.ToolTip>

    <ToolTip Placement="Right"

             PlacementRectangle="50,0,0,0"

             HorizontalOffset="10"

             VerticalOffset="20"

             HasDropShadow="false"

             Opened="whenToolTipOpens"

             Closed="whenToolTipCloses"             >

      <BulletDecorator>

        <BulletDecorator.Bullet>

         <Ellipse Height="10" Width="10" Fill="Red"/>

        </BulletDecorator.Bullet>

        <TextBlock>Uses the ToolTip Class</TextBlock>

      </BulletDecorator>

    </ToolTip>

 </Ellipse.ToolTip>

</Ellipse>

</Grid>

CS代码中,首先声明一个Ellipse,然后判断sender的类型是否是一个Ellipse,如果是,就把这个Ellipse的填充色变为Blue。如果sender为一个ToolTip,首先声明一个ToolTip,然后把sender赋值于它,然后得到父控件,然后再得到布局控件,并把它转化成Ellipse,并把Ellipse的填充色变为Blue

whenToolTipCloses则是相反的过程。

CS代码:

 void whenToolTipOpens(object sender, RoutedEventArgs e)

        {

            Ellipse ell = new Ellipse();

            if (sender.GetType().FullName.Equals("System.Windows.Shapes.Ellipse"))

            {

                ell = (Ellipse)sender;

                ell.Fill = Brushes.Blue;

            }

            else if (sender.GetType().FullName.Equals(

                                     "System.Windows.Controls.ToolTip"))

            {

                ToolTip t = (ToolTip)sender;

                Popup p = (Popup)t.Parent;

                ell = (Ellipse)p.PlacementTarget;

                ell.Fill = Brushes.Blue;

            }

        }

 

        void whenToolTipCloses(object sender, RoutedEventArgs e)

        {

            Ellipse ell = new Ellipse();

            if (sender.GetType().FullName.Equals(

                              "System.Windows.Shapes.Ellipse"))

            {

                ell = (Ellipse)sender;

                ell.Fill = Brushes.Gray;

            }

            else if (sender.GetType().FullName.Equals(

                                   "System.Windows.Controls.ToolTip"))

            {

                ToolTip t = (ToolTip)sender;

                Popup p = (Popup)t.Parent;

                ell = (Ellipse)p.PlacementTarget;

                ell.Fill = Brushes.Gray;

            }

        }

posted @ 2007-05-07 13:54  桂素伟  阅读(2512)  评论(0编辑  收藏  举报