Pinotek Pinotek - 1 month ago 7
C# Question

ToolTip as Timer

My tooltip should show how long my program is running. So I try to add +1 to my tooltip, but that doesn't work.
That is my xaml code:

<StatusBarItem >
<Image ToolTip="{Binding Path=ToolTipStatus}"/>
</StatusBarItem>


And thats my C# code:

private string _toolTipStatus = "0";
private string ToolTipStatus
{
get { return _toolTipStatus; }
}

private void Example()
{
_toolTipStatus = _toolTipStatus + 1;
}

Answer

First, nowhere in this code is there any reason for the UI to guess when or if your private field has changed. Second, your property is private too, so the UI can't see it either. Finally, repeatedly appending "1" to a string is going to get you a string that looks like "11111111111111111111111111111" after the timer fires a few times. If that's what you want, that's fine, but I think it might not be.

public class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class MyViewModel : ViewModelBase
{

    private int _toolTipStatus = 0;
    private int ToolTipStatus
    {
        get { return _toolTipStatus; }
        protected set {
            if (_toolTipStatus != value)
            {
                _toolTipStatus = value;
                OnPropertyChanged(nameof(ToolTipStatus));
            }
        }
    }
}


private void Example()
{            
    ToolTipStatus += 1;
}

You won't say if you've got a viewmodel. You won't say what class your code is in or how (or if) it gets called. All your properties are private. You won't say what the XAML looks like or even if there is any. I sense a theme of obsessive secrecy here. You need to learn when to open up and share.

And you need a viewmodel, and you need it to implement INotifyPropertyChanged.