繁体   English   中英

如何在WPF中绑定到集合中的集合

[英]How to bind to a collection within a collection in WPF

我仍处于学习WPF的早期阶段,因此决定尝试编写一个相当简单的Contact Browser应用程序以掌握基础知识。 为了增加复杂性,我正在使用另一个应用程序中的对象。

到目前为止,我已经能够成功将ListBox控件绑定到Collection并显示联系人姓名。 在屏幕中间,我有一个带有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>

这是BasicContactCard的XAML:

<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>

除Phone以外,BasicContactCard中除Phone之外的所有元素都作为可获取属性公开,该属性与列表框绑定到的Contact集合对象有关,但Phone除外,它位于可以在C#中调用的Field对象的集合中

Contact c = contacList[i];
string val = c.ContactFields.Item("Phone",FieldNameType.Alias);

我希望所有这些都有意义! 任何帮助或指向资源的指针将不胜感激!

VIV

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

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();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM