質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

1回答

6848閲覧

WPFで画面が開かれてからの経過時間を画面にリアルタイムで表示させたい

退会済みユーザー

退会済みユーザー

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

1クリップ

投稿2018/06/30 14:59

編集2022/01/12 10:55

WPFアプリケーションの画面上にその画面が表示されてからの経過時間をリアルタイムで表示させたいのですが、どうやって実装したらいいかがわかりません。

XAML

1<Window x:Class="Application.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:Application" 7 mc:Ignorable="d" 8 Title="title" Height="450" Width="800"> 9 <Grid> 10 <Label x:Name="Time_Label" Content="{Binding time}" /> 11 </Grid> 12</Window>

C#

1using System; 2using System.Windows; 3using System.Windows.Navigation; 4using System.Diagnostics; 5 6namespace Application 7{ 8 public Stopwatch sw; 9 10 private class BindingData 11 { 12 public string time { get; set; } = "00:00:00.00"; 13 } 14 15 /// <summary> 16 /// MainWindow.xaml の相互作用ロジック 17 /// </summary> 18 public partial class MainWindow : Window 19 { 20 public MainWindow() 21 { 22 InitializeComponent(); 23 sw = new Stopwatch(); 24 sw.Start(); 25 this.DataContext = new BindingData(); 26 } 27 } 28} 29

sw.Elapsed.ToString("hh:mm:ss.ff")を常にリアルタイムでLabelに出力させたいのですが通知のさせ方がよくわかりません。
どなたかご助力をお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

C#

1 public partial class MainWindow : Window 2 { 3 private DateTime startTime; 4 private System.Windows.Threading.DispatcherTimer timer; 5 6 public MainWindow() 7 { 8 InitializeComponent(); 9 this.DataContext = new BindingData(); 10 ContentRendered += (s,e) => { 11 startTime = DateTime.Now; 12 timer = new System.Windows.Threading.DispatcherTimer(); 13 timer.Tick += new EventHandler(timerTick); 14 timer.Interval = new TimeSpan(0,0,1); 15 timer.Start(); 16 }; 17 } 18 19 private timerTick() 20 { 21 BindingData data = this.DataContext as BindingData; 22 DateTime d = DateTime.Now - startTime; 23 data.time = d.toShortTimeString() + "." + d.Millisecond; 24 } 25 }

未検証ですが、多分こんな感じでいけるのでは。

投稿2018/06/30 15:53

tkanda

総合スコア2425

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/07/01 00:09

結構遠回りなことしないとできないんですね・・・
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問