2012-10-27 2 views
1

시간이 많이 드는 특정 작업의 상태를 표시하는 WPF 응용 프로그램을 작성하고 있습니다.변수를 WPF 바인딩에 전달하려면 어떻게해야합니까?

나는 작업의 시작 시간에 바인딩 된 TextBlock을 가지고 있으며, 나는 그 작업에 소요 된 시간을 표시하기위한 또 다른 TextBlock을 가지고있다.

변환기를 사용하여 간단한 바인딩을 사용하여 두 번째 TextBlock에 TimeSpan을 올바르게 표시 할 수 있었지만이 솔루션에 너무 열중하지 않았습니다. 시간이 지남에 따라 두 번째 TextBlock에서 TimeSpan을 업데이트하고 싶습니다.

예를 들어 사용자가 응용 프로그램을로드하면 두 번째 TextBlock은 "5 분"이라고 말하지만 15 분 후 "5 분"이라고 말하면 오해의 소지가 있습니다.

나는이 솔루션 (Binding to DateTime.Now. Update the value)을 발견 할 수 있었는데, 이는 내가 원했던 것만 큼 가까이에 있었지만 그다지 좋지 않았습니다.

이 Ticker 클래스에 작업의 시작 시간을 전달하고 텍스트를 업데이트 할 수있는 방법이 있습니까?

편집 : 여기에 지금까지이 작업은 다음과 같습니다
C# :
공용 클래스 시세 :에서 INotifyPropertyChanged { 공공 정적 날짜 시간 상영 {얻을; 세트; }

public Ticker() 
    { 
     Timer timer = new Timer(); 
     timer.Interval = 1000; 
     timer.Elapsed += timer_Elapsed; 
     timer.Start(); 
    } 

    public string TimeDifference 
    { 
     get 
     { 
      string timetaken = ""; 
      try 
      { 
       TimeSpan ts = DateTime.Now - StartTime; 
       timetaken = (StartTime == default(DateTime)) ? "StartTime not set!" : ((ts.Days * 24) + ts.Hours).ToString("N0") + " hours, " + ts.Minutes + " minutes, " + ts.Seconds + " seconds"; 
      } 
      catch (Exception) { } 

      return timetaken; 
     } 
    } 

    private void timer_Elapsed(object sender, ElapsedEventArgs e) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs("TimeDifference")); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

XAML :
<StackPanel Grid.Row="2" Grid.Column="0" Orientation="Horizontal">
<TextBlock Text="Dataset started on:"/>
<TextBlock Name="starttimeLabel" Text="10/23/2012 4:42:26 PM"/>
</StackPanel>
<StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal">
<TextBlock Text="Time taken:"/>
<TextBlock Name="timespanLabel" Text="{Binding Source={StaticResource ticker}, Path=TimeDifference, Mode=OneWay}"/>
</StackPanel>

마지막으로, 내 StatusPanel의로드 이벤트에서, 나는 다음과 같습니다
Ticker.StartTime = Convert.ToDateTime (starttimeLabel.Text);

나는 극악한 형식을 드려 죄송합니다,하지만 난 (다음 삽입 코드 블록 4 개 공간을 <code></code> 등을 사용하여 시도) 아무 소용이 지침에 따라 시도

+0

과제 란 무엇을 의미합니까? 몇 가지 코드를 추가 할 수 있습니까? –

+0

이 컨텍스트에서 "작업"의 정의는 대답에 영향을 미치지 않습니다. 그러나 작업에는 여러 컴퓨터에서 수백만 개의 이미지가 생성되므로 며칠이 걸릴 수 있습니다. 이것이 내가 작업이 처리되는 시간을 표시하지 않는 이유입니다. –

답변

0

하는 카운트 다운 "으로"DispatcherTimer를 사용해보십시오 :

XAML :

<Window x:Class="CountDown.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
    <TextBlock x:Name="initialTime" Width="50" Height="20" Margin="165,66,288,224"></TextBlock> 
    <TextBlock x:Name="txtCountDown" Width="50" Height="20" Margin="236,66,217,225"></TextBlock> 
    </Grid> 
</Window> 

C 번호 :

namespace CountDown 
    { 
     /// <summary> 
     /// Interaction logic for MainWindow.xaml 
     /// </summary> 
     public partial class MainWindow : Window 
     { 
      private DispatcherTimer cDown; 

      public MainWindow() 
      { 
       InitializeComponent(); 
       initialTime.Text = DateTime.Now.ToString("HH:mm:ss"); 
       TimeSpan timeToDie = new TimeSpan(0, 0, 10); //Set the time "when the users load" 
       txtCountDown.Text = timeToDie.ToString(); 

       cDown = new DispatcherTimer(); 
       cDown.Tick += new EventHandler(cDown_Tick); 
       cDown.Interval = new TimeSpan(0, 0, 1); 
       cDown.Start(); 
      } 


      private void cDown_Tick(object sender, EventArgs e) 
      { 
       TimeSpan? dt = null; 
       try 
       { 
        dt = TimeSpan.Parse(txtCountDown.Text); 
        if (dt != null && dt.Value.TotalSeconds > 0 ) 
        { 
         txtCountDown.Text = dt.Value.Add(new TimeSpan(0,0,-1)).ToString(); 
        } 
        else 
        { 
         cDown.Stop(); 
        } 
       } 
       catch (Exception ex) 
       { 
        cDown.Stop(); 
        Console.WriteLine(ex.Message); 
       } 


      } 

     } 
    } 
+0

이것은 제가 찾고있는 것에 가깝지만 꽤 아닙니다. 마크 업에서 바인딩을 통해이 작업을 수행 할 수 있기를 바랬습니다. –

관련 문제