저는 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