我已经使用ToolTip with Behavior。所以,当点击控件时,将出现工具提示。如何只在一个控件中设置工具提示?
但是,当更改控件的禁用时,此工具提示未出现。 因为鼠标事件未被激活。所以,我使用了ContentContorl,它可以在更改禁用时激活鼠标事件。
但是,当我在一个网格中有几个控件,我不知道如何激活唯一的一个控件。
<Window.Resources>
<Style TargetType="ContentControl" >
<Setter Property="localToolTip:ToolTipTouchScreenBehavior.IsEnabled" Value="True"/>
</Style>
<Style TargetType="TextBlock" >
<Setter Property="localToolTip:ToolTipTouchScreenBehavior.IsEnabled" Value="True"/>
</Style>
</Window.Resources>
<ContentControl ToolTip="This is ToolTip5 Test.">
<Grid IsEnabled="False">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock HorizontalAlignment="Left" Margin="5" TextWrapping="Wrap" Text="ToolTip Test5" FontSize="25"
ToolTipService.ShowOnDisabled="True" />
<TextBox Grid.Column="1" Width="200" />
</Grid>
</ContentControl>
public class ToolTipTouchScreenBehavior : Behavior<FrameworkElement>
{
public static DependencyProperty IsEnabledProperty =
DependencyProperty.RegisterAttached("IsEnabled", typeof(bool),
typeof(ToolTipTouchScreenBehavior), new FrameworkPropertyMetadata(false, OnIsEnabledChanged));
public static bool GetIsEnabled(DependencyObject uie)
{
return (bool)uie.GetValue(IsEnabledProperty);
}
public static void SetIsEnabled(DependencyObject uie, bool value)
{
uie.SetValue(IsEnabledProperty, value);
}
public static void OnIsEnabledChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs e)
{
UIElement uie = dpo as UIElement;
if (uie != null)
{
var behColl = Interaction.GetBehaviors(uie);
var existingBehavior = behColl.FirstOrDefault(b => b.GetType() ==
typeof(ToolTipTouchScreenBehavior)) as ToolTipTouchScreenBehavior;
if ((bool)e.NewValue == false && existingBehavior != null)
behColl.Remove(existingBehavior);
else if ((bool)e.NewValue == true && existingBehavior == null)
behColl.Add(new ToolTipTouchScreenBehavior());
}
}
Timer timer { get; set; }
ToolTip toolTip { get; set; }
protected override void OnAttached()
{
base.OnAttached();
toolTip = new ToolTip();
timer = new Timer();
timer.Interval = 5000;
timer.Elapsed += OnTimerElapsed;
AssociatedObject.MouseLeave += OnMouseLeave;
AssociatedObject.MouseLeftButtonUp += OnMouseLeftButtonUp;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.MouseLeave -= OnMouseLeave;
AssociatedObject.MouseLeftButtonUp -= OnMouseLeftButtonUp;
}
public void OnMouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
CloseToolTip();
}
public void OnMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
try
{
//var temp = AssociatedObject.ToolTip;
//if (((dynamic)sender).ToolTip != null)
if (AssociatedObject.ToolTip != null)
{
if (AssociatedObject.ToolTip is string)
toolTip.Content = AssociatedObject.ToolTip;
else
toolTip = (ToolTip)AssociatedObject.ToolTip;
//Debug.WriteLine("ToolTip Opened: {0}, ToolTip Value: {1}", toolTip.IsOpen, toolTip.ToolTip);
toolTip.IsOpen = true;
timer.Start();
}
}
catch (Exception ex)
{
throw ex;
}
}
private void CloseToolTip()
{
if (timer.Enabled)
{
timer.Stop();
}
if (toolTip != null)
{
toolTip.IsOpen = false;
}
}
private void OnTimerElapsed(object sender, ElapsedEventArgs e)
{
Application.Current.Dispatcher.BeginInvoke((Action)CloseToolTip, DispatcherPriority.Send);
}
}
当我有上面的代码,我只是想添加工具提示仅用于TextBlock。不是TextBox。
如何将ToolTip应用于一个特殊控件?
如果我理解正确的话,你应该问如何对鼠标事件作出反应上禁用的控制,以激活工具提示? – grek40