我是新來的C#和xaml。下面的一種方法工作,另一種方法不適用。他們看起來和我一樣,都返回ObservableCollection。 如果有人告訴我用於這些功能的差異或術語,我也可以查找它。請提前感謝您。xaml綁定 - 函數差異
//This one works when binded with xaml and displays the
public ObservableCollection<Emp> Items
{
get
{
collection.Add(new Emp { Name = "Emp1", Age = 21 });
collection.Add(new Emp { Name = "Emp2", Age = 22 });
return collection;
}
}
//This one does not work
public ObservableCollection<Emp> collection = new ObservableCollection<Emp>();
public ObservableCollection<Emp> Items()
{
collection.Add(new Emp { Name = "Emp1", Age = 21 });
collection.Add(new Emp { Name = "Emp2", Age = 22 });
return collection;
}
<!-- XAML CODE -->
<Grid>
<ListBox Margin="20" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text="{Binding Path=Age}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
謝謝你們倆! – Ozland 2014-10-02 23:16:54