Question

I have something like this in xaml:

<Button Content="{Binding MyStopwatch.IsRunning,
        Converter={StaticResource BoolToStr}}"/>

I need to display Start, when IsRunning is false and Stop, when IsRunning is true. I have no problem with converter or binding itself.

I have problem with refreshing IsRunning property. When IsRunning property change while programm is running - it does not change Start/Stop text.

I know how to implement INotifyPropertyChange on my own properties. But I dont know how to implement (something like) property change on IsRunning

Was it helpful?

Solution

If you want to update your bindings, you can call the PropertyChanged on property MyStopwatch whenever you start or stop a stopwatch.

OnPropertyChanged("MyStopwatch");

OTHER TIPS

You can't make StopWatch implement INotifyPropertyChanged. What you can do, is create your own wrapper for it, and use that instead. For example:

public class StopwatchWrapper : INotifyPropertyChanged
{
    Stopwatch _stopwatch;

    private bool _isRunning;
    public bool IsRunning
    {
        get { return _isRunning; }
        set
        {
            if (_isRunning != value)
            {
                _isRunning = value;
                OnPropertyChanged("IsRunning");
            }
        }
    }

    public StopwatchWrapper()
    {
        _stopwatch = new Stopwatch();
        _isRunning = false;
    }

    public void Start()
    {
        _stopwatch.Start();
        IsRunning = _stopwatch.IsRunning;
    }

    public void Stop() 
    {
        _stopwatch.Stop();
        IsRunning = _stopwatch.IsRunning;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top