Silverlight的自定义tooltip提示工具条
生活随笔
收集整理的這篇文章主要介紹了
Silverlight的自定义tooltip提示工具条
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這種應(yīng)用場景其實很多,比如游戲中裝備/魔法的選擇菜單,這里借用了"深藍色右手"的一張圖
?再比如聊天室中的文本顏色設(shè)置?
雖然sl的ToolTipService.ToolTip屬性可以設(shè)置任何對象,比如下面這樣
代碼 1?<Rectangle?Fill="Red"?Height="50"?Width="50"?ToolTipService.Placement="Top">2?????????????<ToolTipService.ToolTip>
3?????????????????<StackPanel?Orientation="Horizontal">
4?????????????????????<Rectangle?Fill="Green"?Height="50"?Width="50"></Rectangle>
5?????????????????????<Rectangle?Fill="Blue"?Height="50"?Width="50"?Margin="1,0,0,0"></Rectangle>
6?????????????????????<Rectangle?Fill="Pink"?Height="50"?Width="50"?Margin="1,0,0,0"></Rectangle>
7?????????????????</StackPanel>
8?????????????</ToolTipService.ToolTip>???????????
9?????????</Rectangle>
?但是有一個問題,鼠標一旦離開對象,tooltip就消失了,沒辦法在tooltip工具欄上點選操作。?
所以得換一種思路,可以借助VSM方便的實現(xiàn),設(shè)置好tooltip工具條后,定義二個基本的狀態(tài):Enter ,Leave 即可,Enter狀態(tài)中設(shè)置tooltip對應(yīng)的對象顯示,Leave狀態(tài)中設(shè)置tooltip對象隱藏
示例代碼(Xaml):
????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"?
????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
????xmlns:d="http://schemas.microsoft.com/expression/blend/2008"?xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"?
????mc:Ignorable="d"
????x:Class="tooltipTest.MainPage"
????d:DesignWidth="640"?d:DesignHeight="480">
????<Grid?x:Name="LayoutRoot">
????????<!--視覺狀態(tài)定義區(qū)-->
????????<VisualStateManager.VisualStateGroups>
????????????<VisualStateGroup?x:Name="CommStates">
????????????????<VisualState?x:Name="Enter">
????????????????????<Storyboard>
????????????????????????<ObjectAnimationUsingKeyFrames?BeginTime="00:00:00"?Storyboard.TargetName="itemsTip"?Storyboard.TargetProperty="(UIElement.Visibility)">
????????????????????????????<DiscreteObjectKeyFrame?KeyTime="00:00:00">
????????????????????????????????<DiscreteObjectKeyFrame.Value>
????????????????????????????????????<Visibility>Visible</Visibility>
????????????????????????????????</DiscreteObjectKeyFrame.Value>
????????????????????????????</DiscreteObjectKeyFrame>
????????????????????????</ObjectAnimationUsingKeyFrames>
????????????????????</Storyboard>
????????????????</VisualState>
????????????????<VisualState?x:Name="Leave">
????????????????????<Storyboard>
????????????????????????<ObjectAnimationUsingKeyFrames?BeginTime="00:00:00"?Storyboard.TargetName="itemsTip"?Storyboard.TargetProperty="(UIElement.Visibility)">
????????????????????????????<DiscreteObjectKeyFrame?KeyTime="00:00:00.1">
????????????????????????????????<DiscreteObjectKeyFrame.Value>
????????????????????????????????????<Visibility>Collapsed</Visibility>
????????????????????????????????</DiscreteObjectKeyFrame.Value>
????????????????????????????</DiscreteObjectKeyFrame>
????????????????????????</ObjectAnimationUsingKeyFrames>
????????????????????</Storyboard>
????????????????</VisualState>
????????????</VisualStateGroup>
????????</VisualStateManager.VisualStateGroups>
????????<Canvas?HorizontalAlignment="Center"?VerticalAlignment="Center"?x:Name="cTip"?Height="20"?Width="20"?Cursor="Hand"?MouseLeave="GoToLeave"?MouseEnter="GoToEnter">
????????????<Rectangle?x:Name="rColor"?Fill="Black"?Width="20"?Height="20"?ToolTipService.ToolTip="選擇顏色"/>
????????????<!--tip顯示區(qū)-->
????????????<ItemsControl?x:Name="itemsTip"?Canvas.Top="-21"?Canvas.Left="0"?Visibility="Collapsed">
????????????????<ItemsControl.ItemsPanel>
????????????????????<ItemsPanelTemplate>
????????????????????????<StackPanel?Orientation="Horizontal"/>
????????????????????</ItemsPanelTemplate>
????????????????</ItemsControl.ItemsPanel>
????????????????<ItemsControl.ItemTemplate>
????????????????????<DataTemplate>
????????????????????????<Rectangle?Fill="{Binding?Color}"?ToolTipService.ToolTip="{Binding?Name}"?Width="20"?Height="20"?Margin="0,0,1,0"?MouseLeftButtonDown="ChangeColor"/>
????????????????????</DataTemplate>
????????????????</ItemsControl.ItemTemplate>
????????????</ItemsControl>
????????</Canvas>
????????
????</Grid>
</UserControl>
后端代碼:
代碼 using?System.Collections.Generic;using?System.Windows;
using?System.Windows.Controls;
using?System.Windows.Input;
using?System.Windows.Media;
using?System.Windows.Shapes;
namespace?tooltipTest
{
????public?partial?class?MainPage?:?UserControl
????{
????????List<FillColor>?lstTipsData;
????????public?MainPage()
????????{
????????????InitializeComponent();
????????????//初始化數(shù)據(jù)
????????????lstTipsData?=?new?List<FillColor>()?{?
????????????????new?FillColor(){?Color?=?new?SolidColorBrush(Colors.Red),?Name="紅色"},
????????????????new?FillColor(){?Color?=?new?SolidColorBrush(Colors.Blue),?Name="藍色"},
????????????????new?FillColor(){?Color?=?new?SolidColorBrush(Colors.Green),Name="綠色"},
????????????????new?FillColor(){?Color?=?new?SolidColorBrush(Colors.Magenta),?Name="洋紅"},
????????????????new?FillColor(){?Color?=?new?SolidColorBrush(Colors.Black),?Name="黑色"},
????????????????new?FillColor(){?Color?=?new?SolidColorBrush(Colors.Orange),?Name="橙色"},
????????????};
????????????this.Loaded?+=?new?RoutedEventHandler(MainPage_Loaded);
????????}
????????void?MainPage_Loaded(object?sender,?RoutedEventArgs?e)
????????{
????????????itemsTip.ItemsSource?=?lstTipsData;?//數(shù)據(jù)綁定???????????
????????}
????????private?void?GoToEnter(object?sender,?MouseEventArgs?e)
????????{
????????????VisualStateManager.GoToState(this,?"Enter",?false);
????????}
????????private?void?GoToLeave(object?sender,?MouseEventArgs?e)
????????{
????????????VisualStateManager.GoToState(this,?"Leave",?false);
????????}
????????///?<summary>
????????///?點擊后更換顏色
????????///?</summary>
????????///?<param?name="sender"></param>
????????///?<param?name="e"></param>
????????private?void?ChangeColor(object?sender,?MouseButtonEventArgs?e)
????????{
????????????rColor.Fill?=?(sender?as?Rectangle).Fill;
????????????VisualStateManager.GoToState(this,?"Leave",?false);
????????}
????}
????///?<summary>
????///?測試實體類
????///?</summary>
????public?class?FillColor
????{
????????public?SolidColorBrush?Color?{?set;?get;?}
????????public?string?Name?{?set;?get;?}
????}
}
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/yjmyzz/archive/2009/12/12/1622697.html
總結(jié)
以上是生活随笔為你收集整理的Silverlight的自定义tooltip提示工具条的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决IE6透明PNG图片的代码
- 下一篇: photos怎么改成中文_Win10怎么