爲了這個原因,我對C#和Windows Phone 7相當陌生,但不管怎樣,我已經爲自己製作一個小應用程序而陷入困境。這是我的問題:DataTemplate的使用
我試圖設置一個DataTemplate,它將定位我在MainPage.xaml.cs中聲明的Name和Drinks變量。這裏是我的動作被點擊按鈕1時:
private void button1_Click(object sender, RoutedEventArgs e)
{
string Name = participantName.Text;
int Drinks = 0;
listBox1.Items.Add(Name + Drinks);
}
這裏是從MainPage.xaml中
<ListBox Height="Auto" HorizontalAlignment="Stretch" Margin="7,74,0,0" Name="listBox1" VerticalAlignment="Stretch" Width="Auto">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="132">
<TextBlock Text="{Binding Path=Name}" FontSize="35" />
<StackPanel Width="370">
<TextBlock Text="{Binding Path=Drinks}" FontSize="35" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我的DataTemplate的問題是,我的數據不會顯示。它在沒有DataTemplate的情況下可以很好地工作,但只要我使用它,我的文本根本無法通過。非常感激你的幫助。
我不知道它是否編譯,但我認爲你需要寫listBox1.Items.Add(新{Name = Name,Drinks = Drinks});所以試試吧。如果它有效,那麼你就不必通過定義一個額外的類來搞亂你的代碼。 – 000 2012-03-13 00:16:18