【发布时间】:2015-09-06 11:47:08
【问题描述】:
我正在开发一个 Windows 商店应用程序。我所做的是,我创建了一个类,ContactBook,其中包含一些属性、字段和构造函数。然后我列了一个清单
List<ContactBook>
我将课程添加到的位置。我想将几个文本块和一个图像绑定到列表中,以便每个显示它们各自的值。到目前为止,我已经创建了以下代码:
班级
public class ContactBook
{
#region _Fields
private string _Name;
private string _Surname;
private string _Number;
private string _ImagePath;
#endregion
#region Constructors
public ContactBook(string name, string surname, string number, string imagePath)
{
ImagePath = imagePath;
Name = name;
Surname = surname;
Number = number;
}
public ContactBook()
{
ImagePath = null;
Name = null;
Surname = null;
Number = null;
}
#endregion
#region Properties
public string Name
{
get
{
return _Name;
}
set
{
this._Name = value;
}
}
public string Surname
{
get
{
return _Surname;
}
set
{
this._Surname = value;
}
}
public string Number
{
get
{
return _Number;
}
set
{
this._Number = value;
}
}
public string ImagePath
{
get
{
return _ImagePath;
}
set
{
this._ImagePath = value;
}
}
#endregion
}
XAML
<Page
x:Class="Summative_LU08.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Summative_LU08"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:myNS="using:Summative_LU08"
mc:Ignorable="d" Loaded="Page_Loaded">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="Contacts" VerticalAlignment="Top" FontFamily="Segoe UI" FontSize="72"/>
<TextBlock HorizontalAlignment="Left" Height="7" Margin="1452,740,-101,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="15"/>
<TextBlock x:Name="Name_Text" Text="Name: " FontSize="20" Margin="180,124,1112,620"/>
<TextBlock x:Name="theName" Text="{Binding }" FontSize="20" Margin="180,153,939,585"/>
<TextBlock x:Name="Surname" Text="Surname: " FontSize="20" Margin="180,208,1087,529"/>
<TextBlock x:Name="theSurname" Text="" FontSize="20" Margin="180,244,1079,496"/>
<TextBlock x:Name="thenumber" Text="" FontSize="20" Margin="10,308,1246,426"/>
<Image Width="160" HorizontalAlignment="Left" Margin="10,124,0,496"/>
<Image Width="160" HorizontalAlignment="Left" Margin="10,369,0,251"/>
<TextBlock x:Name="ContactNumber" Text="Contact Number:" Width="160" HorizontalAlignment="Left" FontSize="20" Height="31" FontStyle="Normal" FontFamily="Segoe UI" Margin="10,277,0,460"/>
<TextBlock x:Name="Name_2" Text="Name: " FontSize="20" Margin="182,369,1110,375"/>
<TextBlock x:Name="theName_2" Text="" FontSize="20" Margin="182,398,1127,337"/>
<TextBlock x:Name="Surname_2" Text="Surname: " FontSize="20" Margin="182,453,1085,284"/>
<TextBlock x:Name="theSurname_2" Text="" FontSize="20" Margin="182,489,1077,251"/>
<TextBlock x:Name="ContactNumber_2" Text="Contact Number:" Width="160" HorizontalAlignment="Left" FontSize="20" Height="31" FontStyle="Normal" FontFamily="Segoe UI" Margin="10,522,0,215"/>
<TextBlock x:Name="thenumber_Copy" Text="" FontSize="20" Margin="10,553,1246,181"/>
</Grid>
</Page>
代码隐藏
private void Page_Loaded(object sender, RoutedEventArgs e)
{
List<ContactBook> contactsBook = new List<ContactBook>();
ContactBook contactBook_1 = new ContactBook();
ContactBook contactBook_2 = new ContactBook();
contactBook_1.Name = "Jaco";
contactBook_1.Surname = "Badenhorst";
contactBook_1.Number = "0728568956";
contactBook_1.ImagePath = "Assets\\Contact";
contactBook_2.Name = "Dean";
contactBook_2.Surname = "Lukas";
contactBook_2.Number = "0825653565";
contactBook_2.ImagePath = "Assets\\Contact";
contactsBook.Add(contactBook_1);
contactsBook.Add(contactBook_2);
theName.SetBinding(contactsBook, contactsBook[0]);
}
如何将文本块绑定到列表,以便theName 文本块显示名称等。 “the”前面的所有文本块名称只是标签,另一个将保存实际值。
【问题讨论】:
标签: c# xaml windows-store-apps