Bonjour,
Mon problème est le suivant:
J'ai un processus long qui s'execute et j'aimerai, pendant le déroulement de ce processus, remonter des informations sur l'activité en cours.
j'ai donc Bindé une collection d'objet TaskItem à une ListBox. Une tache ( Action globale) contient une liste de sous-tâches (information sur ce qui est executé par le processus long).
Afin de laissé la Thread UI tranquille, mon processus long s'execute bien evidement sur une autre Thread. Du coup, lorsque je met à jour la collection de sous-taches, une exception est générée car la modification est faite par une autre thread.
Je sais que je dois utiliser un dispatcher, mais je ne sais pas comment l'exploiter au travers du binding, les exemples que j'ai trouvé mettant toujours en oeuvre des propriétés directe d'un control. (TextBox.text).
Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public class TaskItem : INotifyPropertyChanged { private String mActionTask; private SubTaskItems mSubItems = new SubTaskItems(); private eTaskState mState = eTaskState.Unknown; public enum eTaskState { Unknown, Executing, Completeted, Failed } public eTaskState State { get { return mState; } set { mState = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("State")); } } public TaskItem(String actionTask) { mActionTask = actionTask; } public void addSubTask(eVerboseLevel level, String msg) { SubTask.Add(new SubTaskItem(msg)); } public String ActionTask { get { return mActionTask; } } public SubTaskItems SubTask { get { return mSubItems; } } public override string ToString() { return mActionTask; } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; #endregion }
Ci-dessous, la reprsésentation de mes ListBoxItem.
Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 <DataTemplate x:Key="ActionTaskItem"> <Grid > <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Image Name="mCurrentStateImage" Source="{Binding State, Converter={StaticResource taskItemStateConverter} }" Margin="5" Grid.Column="0" Grid.RowSpan="2" Width="24" Height="24" Stretch="Uniform" VerticalAlignment="Top" /> <TextBlock Text="{Binding ActionTask}" Margin="5" Grid.Row="0" Grid.Column="1" Style="{StaticResource ListBoxItemActionTitle}"/> <Expander Header="{Binding SubTask.LastSubTaskItem}" Style="{StaticResource ListBoxItemActionDetail}" IsExpanded="false" Margin="25,0,5,0" Grid.Row="1" Grid.Column="1" > <ListBox ItemsSource="{Binding SubTask}" Margin="25,0,25,0" Background="Transparent"/> </Expander> </Grid> </DataTemplate>
Merci de m'eclairer,
Cordialement,
Ahryman40k.
Partager