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.IsEnabledAttachedプロパティを使用できます。

<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 に答える