2010-06-16 92 views
2

我仍然处于学习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

+0

什么类型的集合是这样的'ContactFields'什么这样做'项目( “手机”,FieldNameType.Alias)' – Amsakanna 2010-06-16 09:38:21

+0

@Veer , ContactFields集合确实公开IEnumerator。 Item方法返回Item的值(作为字符串)。即对于字段集合中的联系人c获取在电话字段中保存的值。 – VivyG 2010-06-16 12:07:15

回答

1

使用值转换器获取电话号码。

XAML:

<UserControl.Resources>  
    <TestApp:PhoneNumberConverter x:Key="PhoneNumberConverter" /> 
</UserControl.Resources> 

<TextBlock Text="{Binding ., Converter=StaticResource PhoneNumberConverter}}"/> 

后面的代码:

public class PhoneNumberConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     Contact c = value as Contact; 
     string phoneNr = c.ContactFields.Item("Phone", FieldNameType.Alias); 
     return phoneNr; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
+0

现在我觉得很无知,我会在哪里放置标签?我是否将它放在ContactWindow或BasicContactCard的xaml中? BasicContactCard是一个UserControl。如果这是一个非常愚蠢的问题,我很抱歉! – VivyG 2010-06-17 08:06:42

+0

BasicContactCard需要转换器将Contact对象转换为电话号码字符串。所以后面的代码和作为资源的声明放在BasicContactCard中。我编辑了代码以表明这一点。 – 2010-06-17 13:00:40

+0

嗯,好吧,我不知道我在这里做错了什么,但它必须是相当基本的东西!在标记我假设TestApp是SLN /项目的名称,在我的情况下是Test.WPF.BasicContcatSearch1,但是我得到一个编译错误,说Type ..没有找到 – VivyG 2010-06-17 13:24:05