我仍然处于学习WPF的早期阶段,并决定尝试编写一个相当简单的联系人浏览器应用程序来掌握基本原理。为了增加复杂性,我使用另一个应用程序中的对象。如何绑定到WPF集合中的集合
到目前为止,我已经能够成功地将ListBox控件绑定到集合并显示联系人名称。在屏幕中间,我有一个带有CustomControl的StackPanel,它显示了Contact的更多细节。除了联系人的对象模型隐藏了字段集合中的PhoneNUmber字段这一事实之外,这一切都非常有效。
如何绑定/调用绑定对象集合的集合中的特定项目?
下面是一些我的XAML的,首先主要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>
这里是XAML的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>
所有除电话的BasicContactCard中的元素从列表框绑定的Contact集合对象中作为可获取属性公开,但位于可在C#中调用的Field对象集合内的Phone除外
Contact c = contacList[i];
string val = c.ContactFields.Item("Phone",FieldNameType.Alias);
我希望一切都有道理!任何帮助或指向资源将非常感激!
Viv
什么类型的集合是这样的'ContactFields'什么这样做'项目( “手机”,FieldNameType.Alias)' – Amsakanna 2010-06-16 09:38:21
@Veer , ContactFields集合确实公开IEnumerator。 Item方法返回Item的值(作为字符串)。即对于字段集合中的联系人c获取在电话字段中保存的值。 – VivyG 2010-06-16 12:07:15