我正在编写一个 WPF 应用程序,它有很多不同的控件,每个控件都有自己的
ToolTip
。虽然 ToolTips
很有用,但其中一些很长并且很碍事。
我希望能够创建一个按钮,单击该按钮即可启用和禁用应用程序上的所有工具提示。我一直在做的事情似乎是一个非常漫长且不必要的方法。有没有办法快速完成我想要的事情?
您可以尝试添加一个隐式样式,将所有顶级窗口的所有
Visbility
的 ToolTips
属性设置为 Collapsed
。像这样的东西:
private bool _isToolTipVisible = true;
private void Button_Click(object sender, RoutedEventArgs e)
{
Style style = new Style(typeof(ToolTip));
style.Setters.Add(new Setter(UIElement.VisibilityProperty, Visibility.Collapsed));
style.Seal();
foreach (Window window in Application.Current.Windows)
{
if (_isToolTipVisible)
{
window.Resources.Add(typeof(ToolTip), style); //hide
_isToolTipVisible = false;
}
else
{
window.Resources.Remove(typeof(ToolTip)); //show
_isToolTipVisible = true;
}
}
}
您可以将全局工具提示样式添加到应用程序资源中:
<Application.Resources>
<Style TargetType="{x:Type ToolTip}">
<Setter Property="Template" Value="{x:Null}" />
</Style>
</Application.Resources>
这会禁用所有工具提示。
要切换使用以下内容:
Style _style;
void Button_Click(object sender, RoutedEventArgs e)
{
if (_style == null)
{
_style = (Style)Application.Current.Resources[typeof(ToolTip)];
Application.Current.Resources.Remove(typeof(ToolTip));
}
else
Application.Current.Resources.Add(typeof(ToolTip), _style);
}
我需要有可以关闭的自定义工具提示。其他解决方案并没有完全涵盖这一点,所以这就是我最终所做的......
此代码允许您拥有自定义提示,并轻松在应用程序范围内打开和关闭它们。就我而言,我将工具提示可见性保存在用户设置中。该设置应用于主窗口加载,然后在设置更改时更新。
在 App.xaml:
<Style x:Key="{x:Type ToolTip}" TargetType="ToolTip">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToolTip">
<Border Name="Border" BorderThickness="1" Background="PaleGoldenrod" CornerRadius="4" BorderBrush="Black">
<ContentPresenter Margin="4"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
... various other settings
</Style>
<Style x:Key="NoTip" TargetType="ToolTip">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
然后,在我的 MainWindow.xaml.cs 中(但可以去任何您要更改提示可见性的地方):
private Style styleWithTips;
private Style styleNoTips;
...
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Set initial tips on/off
styleWithTips = (Style)Application.Current.Resources[typeof(ToolTip)];
styleNoTips = (Style)Application.Current.Resources["NoTip"];
updateTipStyle();
}
private void Settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "ShowHints")
{
updateTipStyle();
}
}
...
private void updateTipStyle()
{
ResourceDictionary resources = Application.Current.Resources;
bool showHints = Properties.Settings.Default.ShowHints;
if (resources.Contains(typeof(ToolTip)))
{
resources.Remove(typeof(ToolTip));
}
resources.Add(typeof(ToolTip), showHints ? styleWithTips: styleNoTips);
}
这是这个问题后来的回答,但我想可能还是能有效解决问题。我需要打开/关闭已禁用的用户控件的工具提示。这是我的解决方案:
ObservableObject.cs
public class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
/// <summary>
/// Raises this object's PropertyChanged event.
/// </summary>
/// <typeparam name="T">The type of the property that has a new value.</typeparam>
/// <param name="propertyExpression">A Lambda expression representing the property that has a new value.</param>
protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
{
var memberExpr = propertyExpression.Body as MemberExpression;
if (memberExpr == null)
throw new ArgumentException("propertyExpression should represent access to a member");
string memberName = memberExpr.Member.Name;
OnPropertyChanged(memberName);
}
}
BooltoOppositeBoolConverter.cs
public class BoolToOppositeBoolConverter : IValueConverter
{ 公共对象转换(对象值,类型目标类型,对象参数,CultureInfo文化) { if (目标类型!= typeof(bool)) { throw new InvalidOperationException("目标必须是布尔值"); }
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
MainViewModel.cs 片段
public class MainViewModel : ObservableObject
{ 私人布尔_isA1ThruA4Enabled = true;
public bool IsA1ThruA4Enabled
{
get { return _isA1ThruA4Enabled; }
set
{
_isA1ThruA4Enabled = value;
OnPropertyChanged("IsA1ThruA4Enabled");
}
}
主窗口.xaml
<Grid x:Name="gridAnalogVoltsViewingRow" Grid.Row="2"
IsEnabled="{Binding IsA1ThruA4Enabled,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
ToolTip="This control is disabled because no RS11 model connected. "
ToolTipService.ShowOnDisabled="True"
ToolTipService.IsEnabled="{Binding IsA1ThruA4Enabled, Converter={StaticResource InvertBoolConverter} }">
<dataTerminalView:AnalogDisplayValueView x:Name="analogVoltsViewer">
<dataTerminalView:AnalogDisplayValueView.BitmapEffect>
<DropShadowBitmapEffect Color="Black" Direction="315"
Softness="0.5" ShadowDepth="10" Opacity="0.5" />
</dataTerminalView:AnalogDisplayValueView.BitmapEffect>
</dataTerminalView:AnalogDisplayValueView>
</Grid>
我希望这对某人有帮助。