Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <ListView .....
- ItemsSource{Binding PeopleList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}
- ... />
- // ...
- private ObservableCollection<Person> _People;
- public ObservableCollection<Person> People
- { get{return _People;} set{ _People = value; RaisePropertyChange("People");}}
- // ...
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- var sync = SynchronizationContext.Current;
- BackgroundWorker w = new BackgroundWorker();
- w.DoWork+=(_, __)=>
- {
- //sync.Post(p => { button.Content = "Working"; }, null);
- int j = 0;
- for (int i = 0; i < 10; i++)
- {
- j++;
- sync.Post(p => { button.Content = j.ToString(); }, null);
- Thread.Sleep(1000);
- }
- sync.Post(p => { button.Background = Brushes.Aqua; button.Content = "Some Content"; }, null);
- };
- w.RunWorkerAsync();
- }
- <Window x:Class="WpfApplication2.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 x:Name="button" Content="Some Content" Click="Button_Click"/>
- </Grid>
- </Window>
- public void LockAndDoInBackground(Action action, string text, Action beforeVisualAction = null, Action afterVisualAction = null)
- {
- if (IsBusy)
- return;
- var currentSyncContext = SynchronizationContext.Current;
- ActiveThread = new Thread((_) =>
- {
- currentSyncContext.Send(t =>
- {
- IsBusy = true;
- BusyText = string.IsNullOrEmpty(text) ? "Wait please..." : text;
- if (beforeVisualAction != null)
- beforeVisualAction();
- }, null);
- action();
- currentSyncContext.Send(t =>
- {
- IsBusy = false;
- BusyText = "";
- if (afterVisualAction != null)
- afterVisualAction();
- }, null);
- });
- ActiveThread.Start();
- }
- private RelayCommand _SomeCommand;
- public RelayCommand SomeCommand
- {
- get { return _SomeCommand ?? (_SomeCommand = new RelayCommand(ExecuteSomeCommand, CanExecuteSomeCommand)); }
- }
- private void ExecuteSomeCommand()
- {
- Action t = ()=>
- {
- //some action
- };
- LockAndDoInBackground(t, "Generating Information...");
- }
- private bool CanExecuteSomeCommand()
- {
- return SelectedItem != null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement