2016-07-20 34 views
-2

Ich habe Textblock auf meinem UI. Ich möchte etwas Text auf dem Textblock dynamisch anzeigen. Ich habe es wie im folgenden Code beschrieben implementiert. Die Werte werden jedoch nicht dynamisch aktualisiert. Ich sehe nur den letzten aktualisierten Wert für den UI-Textblock. Ich habe eine Verzögerung eingefügt, um die Änderung zu bemerken.Wert von TextBlock in WPF dynamisch aktualisieren [Sovled]

Bitte bieten Sie eine Lösung oder einen Kommentar für weitere Informationen.Vielen Dank im Voraus.

Code: 

namespace TxtBlock 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     SomeObjectClass obj = new SomeObjectClass(); 

     public MainWindow() 
     { 
      InitializeComponent(); 

      txtName.DataContext = obj; 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      obj.Name = "Hello World"; 

      Thread.Sleep(2000); 
      obj.Name = "Third"; 
     } 

    } 

    class SomeObjectClass : INotifyPropertyChanged 
    { 
     private string _name = "hello"; 
     public string Name 
     { 
      get 
      { 
       return _name; 
      } 
      set 
      { 
       _name = value; 
       OnPropertyChanged("Name"); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     public void OnPropertyChanged(string PropertyName) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(PropertyName)); 
     } 
    } 

} 

XAML: <Window x:Class="TxtBlock.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> 
     <Button Content="Button" HorizontalAlignment="Left" Margin="237,170,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/> 
     <TextBlock HorizontalAlignment="Left" Margin="237,256,0,0" TextWrapping="Wrap" x:Name="txtName" Text="{Binding Name}" VerticalAlignment="Top"/> 

    </Grid> 
</Window> 
+0

-Code führt in Millisekunden ... ofcourse u wird nur c dem letzten Wert. Google WPF-Bindung und Verzögerungen. Sie werden eine Menge Materie dort finden – Akanksha

+0

Ja, ich stimme dem zu, aber das Ergebnis ist das gleiche auch mit Verzögerung. Ich versuchte mit 1sec 2sec Verzögerungen, um die Änderung zu beobachten. – Sagar

+0

Sonst bitte antworten. – Sagar

Antwort

0

Sie müssen in Hintergrundthread starten Sie Ihre Werte in UI TextBlock-

-Code zu aktualisieren:

public partial class TextBlockExample : Window 
{ 
    ThreadExampleViewModel viewModel = new ThreadExampleViewModel(); 

    public TextBlockExample() 
    { 
     InitializeComponent(); 
     this.DataContext = viewModel; 
    } 

    private void btnClick_Click(object sender, RoutedEventArgs e) 
    { 
     /// Background thread Thread to run your logic 
     Thread thread = new Thread(YourLogicToUpdateTextBlock); 
     thread.IsBackground = true; 
     thread.Start(); 
    } 

    private void YourLogicToUpdateTextBlock() 
    { 
     /// Example i am updating with i value. 
     for (int i = 0; i < 1000; i++) 
     { 
      viewModel.Name = i + " Conut"; 
      Thread.Sleep(1000); 
     } 
    } 
} 



<Grid> 
    <StackPanel> 
     <TextBlock x:Name="txtName" Text="{Binding Name}" Height="30" Width="100" Margin="10"/> 
     <Button x:Name="btnClick" Content="Click" Height="30" Width="100" Margin="10" Click="btnClick_Click"/> 
    </StackPanel> 
</Grid> 




public class ThreadExampleViewModel : INotifyPropertyChanged 
{ 

    private string name = "Hello"; 

    public string Name 
    { 
     get { return name; } 
     set { name = value; OnPropertyChanged("Name"); } 
    } 


    public event PropertyChangedEventHandler PropertyChanged; 

    public void OnPropertyChanged(string PropertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(PropertyName)); 
    } 
} 
+0

Super Kumpel, Danke. Es klappt. du hast mein Leben gerettet :) – Sagar