我现在正在尝试将数据添加到列表框中。如果您查看我的 XAML,这是我构建的列表框:
<ListBox
Height="517"
HorizontalAlignment="Left"
Margin="12,84,0,0"
Name="searchList"
VerticalAlignment="Top"
Width="438"
SelectionChanged="SearchList_SelectedEvent">
<!-- What each listbox item will look like -->
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding firstName}" FontSize="28" />
<TextBlock Text="{Binding lastName}" FontSize="28" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在我的 .css 类中,该类与包含 listBox 的屏幕关联:
public Search()
{
InitializeComponent();
//The variables that I want to render in the list
string firstName = "John";
string lastName = "Smith";
}
所以我的问题是,数据绑定到底是如何工作的?您可以看到,在 XAML 中我尝试绑定变量,但我不知道这是否有效或者我是否正确执行了操作?
对于初学者来说,XAML 如何知道在哪里可以找到这些变量?
我建议你通过本教程 http://jesseliberty.com/2010/11/10/windows-phone-from-scratch-6-data-binding-really/Tomas Jansson 在另一个答案中建议。
除了你的代码中的内容之外,
首先,您需要创建一个包含数据的 List 或 ObservableCollection,该数据可绑定到 ListBox。
List myList = new List()
{new Name() { firstName = "sdfjsdk", lastName= "sdfsfsjdf"},
new Name() { firstName = "bthbbh", lastName= "ereyyyu"},
new Name() { firstName = "svbfbb", lastName= "sdfertbn"} };
其中名称是
class Name()
{
string firstName;
string lastName;
}
然后在后面的代码中,
myList.ItemsSource = myList; //this lets the XAML from where to fetch the data to bind
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)