我已經使用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