Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. <ListView .....
  2. ItemsSource{Binding PeopleList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}
  3. ... />
  4.  
  5. // ...
  6. private ObservableCollection<Person> _People;
  7. public ObservableCollection<Person> People
  8. { get{return _People;} set{ _People = value; RaisePropertyChange("People");}}
  9. // ...
  10.  
  11. private void Button_Click(object sender, RoutedEventArgs e)
  12. {
  13. var sync = SynchronizationContext.Current;
  14. BackgroundWorker w = new BackgroundWorker();
  15. w.DoWork+=(_, __)=>
  16. {
  17. //sync.Post(p => { button.Content = "Working"; }, null);
  18. int j = 0;
  19. for (int i = 0; i < 10; i++)
  20. {
  21. j++;
  22. sync.Post(p => { button.Content = j.ToString(); }, null);
  23. Thread.Sleep(1000);
  24. }
  25. sync.Post(p => { button.Background = Brushes.Aqua; button.Content = "Some Content"; }, null);
  26. };
  27. w.RunWorkerAsync();
  28. }
  29.  
  30. <Window x:Class="WpfApplication2.MainWindow"
  31. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  32. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  33. Title="MainWindow" Height="350" Width="525">
  34. <Grid>
  35. <Button x:Name="button" Content="Some Content" Click="Button_Click"/>
  36. </Grid>
  37. </Window>
  38.  
  39. public void LockAndDoInBackground(Action action, string text, Action beforeVisualAction = null, Action afterVisualAction = null)
  40. {
  41. if (IsBusy)
  42. return;
  43. var currentSyncContext = SynchronizationContext.Current;
  44. ActiveThread = new Thread((_) =>
  45. {
  46. currentSyncContext.Send(t =>
  47. {
  48. IsBusy = true;
  49. BusyText = string.IsNullOrEmpty(text) ? "Wait please..." : text;
  50. if (beforeVisualAction != null)
  51. beforeVisualAction();
  52. }, null);
  53. action();
  54. currentSyncContext.Send(t =>
  55. {
  56. IsBusy = false;
  57. BusyText = "";
  58. if (afterVisualAction != null)
  59. afterVisualAction();
  60. }, null);
  61. });
  62. ActiveThread.Start();
  63. }
  64.  
  65. private RelayCommand _SomeCommand;
  66. public RelayCommand SomeCommand
  67. {
  68. get { return _SomeCommand ?? (_SomeCommand = new RelayCommand(ExecuteSomeCommand, CanExecuteSomeCommand)); }
  69. }
  70.  
  71. private void ExecuteSomeCommand()
  72. {
  73. Action t = ()=>
  74. {
  75. //some action
  76. };
  77.  
  78. LockAndDoInBackground(t, "Generating Information...");
  79. }
  80.  
  81. private bool CanExecuteSomeCommand()
  82. {
  83. return SelectedItem != null;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement