I'm still at the early stages of learning WPF and have decided to try and write a fairly simple Contact Browser app to get to grips with the fundamentals. To add to the complexity I am using Objects from another application.
我还在学习WPF的早期阶段,并决定尝试编写一个相当简单的联系人浏览器应用程序来掌握基本原理。为了增加复杂性,我使用了来自另一个应用程序的对象。
So far I have been able to successfully bind a ListBox control to a Collection and display the Contact Names. In the middle of the screen I have a StackPanel with a CustomControl that displays further details of the Contact. This all works surprisingly well except for that fact that the Object Model for the Contact hides the PhoneNUmber field within a Collection of fields.
到目前为止,我已经能够成功地将ListBox控件绑定到集合并显示联系人名称。在屏幕中间,我有一个带有CustomControl的StackPanel,它显示联系人的进一步细节。除了联系人的对象模型在字段集合中隐藏了PhoneNUmber字段这一事实之外,所有这些都非常有效。
How can I bind/call a specific item within a collection of a bound object collection?
如何绑定/调用绑定对象集合中的特定项?
Here is some of my XAML, firstly the main ContactWindow:
以下是我的一些XAML,首先是主要内容:
<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>
And here is the XAML for the BasicContactCard:
这是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>
All the elements within the BasicContactCard except for the Phone are exposed as get-able properties from the Contact collection object that the Listbox is bound to except for the Phone which resides within a collection of Field objects that can be called in C# as
除了电话之外,BasicContactCard中的所有元素都作为联系人集合对象的可获取属性公开,列表框绑定到该对象,除了位于可以在c# as中调用的字段对象集合中的电话
Contact c = contacList[i];
string val = c.ContactFields.Item("Phone",FieldNameType.Alias);
I hope all that makes sense! Any help or pointers to resources would be really appreciated!
我希望这一切都是有道理的!如果您有任何帮助或建议,我们将非常感激!
Viv
薇芙
1 个解决方案
#1
1
Use a value converter to get the phone number.
使用值转换器获取电话号码。
XAML:
XAML:
<UserControl.Resources>
<TestApp:PhoneNumberConverter x:Key="PhoneNumberConverter" />
</UserControl.Resources>
<TextBlock Text="{Binding ., Converter=StaticResource PhoneNumberConverter}}"/>
Code behind:
背后的代码:
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();
}
}
#1
1
Use a value converter to get the phone number.
使用值转换器获取电话号码。
XAML:
XAML:
<UserControl.Resources>
<TestApp:PhoneNumberConverter x:Key="PhoneNumberConverter" />
</UserControl.Resources>
<TextBlock Text="{Binding ., Converter=StaticResource PhoneNumberConverter}}"/>
Code behind:
背后的代码:
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();
}
}