Bonjour,

J'ai un xaml qui contient une combobx, celle-ci est remplie via une static Resource tandis que l'item à préselectionner est un attribut du datacontext (instancié par un appel wcf).

Extrait du xaml (disons ContactDetailView.xaml)

Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
 
<TextBox Text="{Binding Firstname, Mode=TwoWay}"/>
<TextBox Text="{Binding Lastname, Mode=TwoWay}"/>
<ComboBox DisplayMemberPath="Name" ItemsSource="{Binding Source={StaticResource dataProvider}, Path=Companies}" SelectedItem="{Binding Company, Mode=TwoWay}" />
<ComboBox DisplayMemberPath="Name" ItemsSource="{Binding Source={StaticResource dataProvider}, Path=Countries}" SelectedItem="{Binding Country, Mode=TwoWay}" />

dataProvider fait référence à une classe déclarée dans le fichier App.xaml : <MyApp:RefDataProvider x:Key="dataProvider"/> 'Companies' et 'Countries' sont des propriétés de cette classe (ObservableCollection) remplies par un appel à WCF.

Le codebehind (ContactDetailView.xaml.cs) :
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
 
 
public partial class ContactDetailView : UserControl
{
private Action<ContactDetailView> closeAction;
 
public ContactDetailView (Contact item, Action<ContactDetailView> closeAction)
{
InitializeComponent();
this.closeAction = closeAction;
/*
appel WCF asynchrone pour récupérer le bon contact (appelons le myContact) et affectation d'une valeur au DataContext avec this.DataContext = myContact;
*/
}
 
private void CloseClick(object sender, RoutedEventArgs e)
{
closeAction(this);
}
 
private void SaveClick(object sender, RoutedEventArgs e)
{
Contact contact;
if ((contact= this.DataContext as Contact) == null)
{
closeAction(this);
return;
}
// save and close
}
}

Le problème : les appels wcf sont asynchrones et 1 fois sur 2 le bon item n'est pas préselectionné dans les combobox, sans doute parceque silverlight essaie de préselectionner la bonne valeur alors que l'appel wcf qui remplit la liste qui sert de source à la remplir la combobox n'est pas encore terminé. Le problème ne se pose pas si dans le 'dataProvider' je renvoie des listes créés en dur (donc de façon instantanée).

Une idée ?

Pour infos le xaml est ouvert depuis une datagrid :

Extrait du ContactList.xaml :

Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 
<data:DataGridTemplateColumn Width="75" >
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Détails" Click="EditClick" Width="70" Tag="{Binding}" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>

Extrait du ContactList.xaml.cs (code behind) :

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
 
private ContactDetailView detailsView;
 
private void EditClick(object sender, EventArgs e)
{
Button button = sender as Button;
if (button != null && button.Tag != null)
{
this.myGrid.Focus(); // workaround to avoid row disappearance, see http://silverlight.net/forums/p/36559/108015.aspx#108015
OpenDetailsView((button.Tag as Contact);
}
}
 
private void OpenDetailsView(Contact contact)
{
detailsView = new ContactDetailView(contact, RemoveDetailsView); // workaround for this bug : https://silverlight.net/forums/p/31071/98644.aspx
detailsView.Visibility = Visibility.Visible;
this.LayoutRoot.Children.Add(detailsView);
}
 
private void RemoveDetailsView(ContactDetailView detailsView)
{
if (detailsView != null)
{
this.LayoutRoot.Children.Remove(detailsView);
}
}