0

I have a button with a tooltip that needs to show when the user hovers. This works fine when a static piece of text is passed, however trying to do it with a dynamic piece of text is proving to be a problem.

currently I have in XAML

 <Button ToolTip="{Binding galue}"></Button>

and in the code behind

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        toolT TT = new toolT();

        TT.galue = "some text";

    }
}

class toolT
{
    public string galue { get; set; }
}

}

also setting the binding to toolT.galue in XAML has no different result

  • 1
    You're not using `INotifyPropertyChanged`. The binding is never told of the change. – DonBoitnott Nov 12 '18 at 17:19
  • You are using code behind, so why to use Binding? You can just give your button a name an access it from code behind to set its tooltip – taquion Nov 12 '18 at 17:36
  • You didn't set the Window's DataContext, like `DataContext = TT;` – Clemens Nov 12 '18 at 17:40
  • @DonBoitnott That's not strictly necessary. OP seems to set the property value only once at startup. – Clemens Nov 12 '18 at 17:41
  • @Clemens I based that on the "static" vs "dynamic" text bit. If it's dynamic, to me that implies setting it more than once. – DonBoitnott Nov 12 '18 at 17:43
  • Besides the DataContext issue pointed out by Clemens, `Binding galue` implies that you have a `galue` property in whatever class you are using as DataContext. You do not have this property in your MainWindow class. – taquion Nov 12 '18 at 17:46
  • Furthermore.. toolT is just a local variable with its scope constrained to your MainWindow constructor – taquion Nov 12 '18 at 17:47
  • @taquion To be a little more precise, `toolT` is a type, and the local variable is `TT`. If you assign it to the Window's DataContext, the Binding works as expected. If you don't set the DataContext, the Binding does *not* try to look up the property in MainWindow. – Clemens Nov 12 '18 at 18:05
  • Yes the datacontext was the problem. Thanks for all the help! – Breytenzest Nov 13 '18 at 10:16

0 Answers0