c# - 如何绑定(bind)到 WPF 集合中的集合

标签 c# wpf binding

我仍处于学习 WPF 的早期阶段,并决定尝试编写一个相当简单的联系人浏览器应用程序来掌握基础知识。为了增加复杂性,我使用了另一个应用程序中的对象。

到目前为止,我已经能够成功地将一个 ListBox 控件绑定(bind)到一个集合并显示联系人姓名。在屏幕中间,我有一个带有 CustomControl 的 StackPanel,它显示联系人的更多详细信息。除了联系人的对象模型将 PhoneNUber 字段隐藏在字段集合中这一事实外,这一切都运行良好。

如何绑定(bind)/调用绑定(bind)对象集合中的特定项目?

这是我的一些 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>

BasicContactCard 中除 Phone 之外的所有元素都作为 Listbox 绑定(bind)到的 Contact 集合对象的可获取属性公开,但 Phone 除外,Phone 位于 Field 对象集合中,可以在 C# 中调用为

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

我希望所有这些都有意义!非常感谢任何帮助或资源指针!

生命

最佳答案

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

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

关于c# - 如何绑定(bind)到 WPF 集合中的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3052045/

相关文章:

c# - 什么是 EF 重写规则?

c# - 扩展TextBox控件并更改部分默认样式

wpf - 将控件绑定(bind)到两个属性

c# - WPF XAML 中的“无法创建”错误的实例

jquery - js-hotkeys - 如何绑定(bind)到?问号

variables - Erlang - 检查未绑定(bind)变量

c# - 如何从窗口获取屏幕截图?

c# - 将对象从 C# 传递到 C++ 得到 "cannot use this indirection"?

c# - 使用 C# 为 Windows 设置 ipv6

wpf - 如何在 WPF 中禁用调整用户控件的大小