Je suis encore aux premiers stades de l'apprentissage de WPF et j'ai décidé d'essayer d'écrire une application Contact Browser assez simple pour me familiariser avec les fondamentaux. Pour ajouter à la complexité, j'utilise des objets d'une autre application.Comment lier une collection au sein d'une collection dans WPF
Jusqu'à présent, j'ai réussi à lier un contrôle ListBox à une collection et afficher les noms de contacts. Au milieu de l'écran, j'ai un StackPanel avec un CustomControl qui affiche plus de détails sur le Contact. Tout cela fonctionne étonnamment bien, sauf que le modèle d'objet pour le contact cache le champ PhoneNUmber dans une collection de champs.
Comment lier/appeler un élément spécifique dans une collection d'une collection d'objets liés?
Voici une partie de mon XAML, d'une part la principale ContactWindow:
<DockPanel Width="auto" Height="auto" Margin="8 8 8 8">
<Border Height="56" HorizontalAlignment="Stretch" VerticalAlignment="Top" BorderThickness="1" CornerRadius="8" DockPanel.Dock="Top" Background="Beige">
<TextBox Height="32" Margin="23,5,135,5" Text="Search for contact here" FontStyle="Italic" Foreground="#FFAD9595" FontSize="14" BorderBrush="LightGray"/>
</Border>
<ListBox x:Name="contactList" DockPanel.Dock="Left" Width="192" Height="auto" Margin="5 4 0 8" ItemsSource="{Binding}" DisplayMemberPath="FullName" />
<Grid DataContext="{Binding ElementName=contactList, Path=SelectedItem}">
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="0.125*" />
</Grid.RowDefinitions>
<local:BasicContactCard Margin="8 8 8 8" />
<Button Grid.Row="1" x:Name="exit" Content="Exit" HorizontalAlignment="Right" Width="50" Height="25" Click="exit_Click" />
</Grid>
</DockPanel>
Et voici le XAML pour la BasicContactCard:
<DockPanel Width="auto " Height="auto" Margin="8,8,8,8" >
<Grid Width="auto" Height="auto" DockPanel.Dock="Top">
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<TextBlock x:Name="companyField" Grid.Row="0" Width="auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="8,8,8,8" Text="{Binding Company}" FontWeight="Bold" FontSize="15" />
<TextBlock x:Name="contactField" Grid.Row="1" Width="auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="8,8,8,8" Text="{Binding FullName}" FontWeight="Bold" />
<TextBlock x:Name="phoneField" Grid.Row="2" Width="auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="8,8,8,8" Text="{Binding Phone}"/>
<TextBlock x:Name="emailField" Grid.Row="3" Width="auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="8,8,8,8" Text="{Binding ValidEmailAddress}"/>
</Grid>
</DockPanel>
Tous les éléments du BasicContactCard à l'exception du téléphone sont Exposé comme propriétés get-able à partir de l'objet de collection Contact auquel la Listbox est liée sauf pour le téléphone qui réside dans une collection d'objets Field pouvant être appelée en C# comme
Contact c = contacList[i];
string val = c.ContactFields.Item("Phone",FieldNameType.Alias);
J'espère que tout cela a du sens! Toute aide ou pointeur vers des ressources serait vraiment apprécié!
Viv
Quel type de collection est ce 'ContactFields' et ce que cela fait' article ("Téléphone", FieldNameType.Alias) ' – Amsakanna
@Veer , La collection ContactFields expose un IEnumerator. La méthode Item renvoie la valeur (en tant que chaîne) de l'élément. c'est-à-dire pour Contact c dans la collection de Champs obtient la valeur détenue dans le champ Téléphone. – VivyG