1

如果我有一个包含将包含工具提示的可见性状态的静态变量的类,我将如何编写代码隐藏以在可见性变量更改时动态更改工具提示的可见性?

即当禁用工具提示选项时,不应该显示任何工具提示,但是当启用工具提示选项时,应该显示工具提示。(工具提示选项保存在不同类的静态变量中)工具提示及其连接的控件是动态创建的。

伪代码:

 ToolTip myToolTip = new ToolTip();
 Visiblity tooltipVis = Visibility.Visible;
 Bind myToolTip.Visiblity to toolTipVis
 //Any control with ToolTip should now show their respective ToolTip messages.
 ...
 tooltipVis = Visibility.Hidden;
 //Any control with ToolTip should now have ToolTip messages disabled

尝试绑定到 TreeViewItem:

 TreeViewItem tvi = new TreeViewItem() { Header = tviHeader };
 ToolTip x = new System.Windows.Controls.ToolTip();
 x.Content = "This is text.";
 Binding binder = new Binding { 
      Source = EnvironmentalVariables.ToolTipVisibility,
      Path = new PropertyPath("Visibility")
 };
 x.SetBinding(VisibilityProperty, binder);
 user.ToolTip = x;

 public class EnvironmentalVariables {
      public static Visibility ToolTipVisibility { get; set; }
 }

这似乎没有将 Visiblity 绑定到 EnvironmentalVariables.ToolTipVisibility 变量。

4

2 回答 2

1

您可以为此使用ToolTipService.IsEnabled 附加属性

<TextBlock Text="Example" ToolTip="This is an example"
           ToolTipService.IsEnabled="{Binding TooltipEnabled, Source={x:Static Application.Current}}">

因为您无法绑定到静态属性(在 WPF 版本 4.5 中您可以),所以我将使用此解决方法从任何地方访问该属性

public partial class App : Application, INotifyPropertyChanged
{
    private bool _tooltipEnabled;
    public bool TooltipEnabled
    {
        get { return _tooltipEnabled; }
        set
        {
            if (_tooltipEnabled != value)
            {
                _tooltipEnabled = value;
                RaiseNotifyPropertyChanged("TooltipEnabled");
            }
        }
    }

    private void RaiseNotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}
于 2012-07-24T18:10:47.577 回答
0

只需删除您创建Path的对象中的属性。Binding这就是它需要工作的全部内容。

  EnvironmentalVariables.ToolTipVisibility = System.Windows.Visibility.Collapsed;

  var b = new Button () { Content = "test" };
  var x = new ToolTip();
  x.Content = "This is text.";
  var binding = new Binding {
    Source = EnvironmentalVariables.ToolTipVisibility,
  };
  x.SetBinding(VisibilityProperty, binding);
  b.ToolTip = x;

如果要在运行时动态更改 ToolTipVisibility,则必须实现属性通知。

于 2012-07-24T18:18:07.223 回答