2010-06-16 96 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