永嘉网站建设,汽车精品设计网站建设,网站开发会什么软件,会展网站建设成功的原因这种应用场景其实很多#xff0c;比如游戏中装备/魔法的选择菜单#xff0c;这里借用了深蓝色右手的一张图 再比如聊天室中的文本颜色设置 虽然sl的ToolTipService.ToolTip属性可以设置任何对象#xff0c;比如下面这样 代码 1 Rectangle FillRed比如游戏中装备/魔法的选择菜单这里借用了深蓝色右手的一张图 再比如聊天室中的文本颜色设置 虽然sl的ToolTipService.ToolTip属性可以设置任何对象比如下面这样 代码 1 Rectangle FillRed Height50 Width50 ToolTipService.PlacementTop2 ToolTipService.ToolTip3 StackPanel OrientationHorizontal4 Rectangle FillGreen Height50 Width50/Rectangle5 Rectangle FillBlue Height50 Width50 Margin1,0,0,0/Rectangle6 Rectangle FillPink Height50 Width50 Margin1,0,0,0/Rectangle7 /StackPanel8 /ToolTipService.ToolTip 9 /Rectangle 但是有一个问题鼠标一旦离开对象tooltip就消失了没办法在tooltip工具栏上点选操作。 所以得换一种思路可以借助VSM方便的实现设置好tooltip工具条后定义二个基本的状态Enter Leave 即可Enter状态中设置tooltip对应的对象显示Leave状态中设置tooltip对象隐藏示例代码(Xaml) 代码 UserControl xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml xmlns:dhttp://schemas.microsoft.com/expression/blend/2008 xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006 mc:Ignorabled x:ClasstooltipTest.MainPage d:DesignWidth640 d:DesignHeight480 Grid x:NameLayoutRoot !--视觉状态定义区-- VisualStateManager.VisualStateGroups VisualStateGroup x:NameCommStates VisualState x:NameEnter Storyboard ObjectAnimationUsingKeyFrames BeginTime00:00:00 Storyboard.TargetNameitemsTip Storyboard.TargetProperty(UIElement.Visibility) DiscreteObjectKeyFrame KeyTime00:00:00 DiscreteObjectKeyFrame.Value VisibilityVisible/Visibility /DiscreteObjectKeyFrame.Value /DiscreteObjectKeyFrame /ObjectAnimationUsingKeyFrames /Storyboard /VisualState VisualState x:NameLeave Storyboard ObjectAnimationUsingKeyFrames BeginTime00:00:00 Storyboard.TargetNameitemsTip Storyboard.TargetProperty(UIElement.Visibility) DiscreteObjectKeyFrame KeyTime00:00:00.1 DiscreteObjectKeyFrame.Value VisibilityCollapsed/Visibility /DiscreteObjectKeyFrame.Value /DiscreteObjectKeyFrame /ObjectAnimationUsingKeyFrames /Storyboard /VisualState /VisualStateGroup /VisualStateManager.VisualStateGroups Canvas HorizontalAlignmentCenter VerticalAlignmentCenter x:NamecTip Height20 Width20 CursorHand MouseLeaveGoToLeave MouseEnterGoToEnter Rectangle x:NamerColor FillBlack Width20 Height20 ToolTipService.ToolTip选择颜色/ !--tip显示区-- ItemsControl x:NameitemsTip Canvas.Top-21 Canvas.Left0 VisibilityCollapsed ItemsControl.ItemsPanel ItemsPanelTemplate StackPanel OrientationHorizontal/ /ItemsPanelTemplate /ItemsControl.ItemsPanel ItemsControl.ItemTemplate DataTemplate Rectangle Fill{Binding Color} ToolTipService.ToolTip{Binding Name} Width20 Height20 Margin0,0,1,0 MouseLeftButtonDownChangeColor/ /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 { ListFillColor lstTipsData; public MainPage() { InitializeComponent(); //初始化数据 lstTipsData new ListFillColor() { 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; //数据绑定 } 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 namesender/param /// param namee/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; } }} 转载于:https://www.cnblogs.com/yjmyzz/archive/2009/12/12/1622697.html