Visual Studio / WPF > ObjectDataProvider > ボタン押下時以降の経過時間の表示 > TimeSpan | string.Format() | ButtonのClick処理

```txt:動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
```

http://gushwell.ldblog.jp/archives/52333863.html
を参考に以下を実装してみた。

- ボタン押下時に処理開始
- 経過時間を表示

使ったものは
- TimeSpan
- string.Format()
- ButtonのClick処理

```MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace _170608_t1408_elapsedTime
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        private MyClock _myClock;       
        public MainWindow()
        {
            InitializeComponent();
            _myClock = this.Resources["keyMyClock"] as MyClock;
            _myClock.Provider = this.Resources["keyElapsed"] as ObjectDataProvider;
        }

        private void B_start_Click(object sender, RoutedEventArgs e)
        {
            _myClock.SetStart();
        }
    }
    public class MyClock
    {
        private System.Timers.Timer _timer = new System.Timers.Timer(); // タイマーイベント用
        private System.DateTime _startTime; // B_start押下時の時刻
        private bool isStart = false; // 経過時間を測定するか
        public ObjectDataProvider Provider { get; set; } // 表示リフレッシュ用

        public MyClock()
        {
            _timer.Interval = 1000; // msec
            _timer.Elapsed += _timer_Elapsed;
            _timer.Enabled = true;
        }
        private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Provider.Refresh();
        }
        public string GetElapsedTime()
        {
            if (isStart)
            {
                TimeSpan ts = DateTime.Now - _startTime;
                return string.Format("{0:000.###}", ts.TotalSeconds);
            }
            return "";
        }
        public void SetStart()
        {
            isStart = true;
            _startTime = DateTime.Now;
        }
    }
}
```

```MainWindow.xaml
<Window x:Class="_170608_t1408_elapsedTime.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_170608_t1408_elapsedTime"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:MyClock x:Key="keyMyClock"/>
        <ObjectDataProvider MethodName="GetElapsedTime" x:Key="keyElapsed"
                            ObjectInstance="{StaticResource keyMyClock}"/>
        <ObjectDataProvider MethodName="SetStart" x:Key="keyStart"
                            ObjectInstance="{StaticResource keyMyClock}"/>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <TextBlock FontSize="18"
            Text="{Binding Source={StaticResource keyElapsed}}"/>
            <Button x:Name="B_start" Content="Start"
                    Click="B_start_Click"/>
        </StackPanel>
    </Grid>
</Window>
```


![2017-06-08_14h42_26.png](https://qiita-image-store.s3.amazonaws.com/0/32870/41777506-c38e-d15a-cc80-1fa1d3b6d296.png)