試す:
xaml:
<StackPanel Grid.Row="1">
<TextBlock Text="{Binding Path=FoodTypes[0].Description}" />
<ItemsControl ItemsSource="{Binding Path=FoodTypes}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Margin="15,0,0,0" Text="{Binding Path=Text}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
コード:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MyViewModel();
}
}
public class FoodType
{
public string Description {get;set;}
public string Text {get;set;}
}
class MyViewModel : INotifyPropertyChanged
{
private ObservableCollection<FoodType> foodTypes;
public ObservableCollection<FoodType> FoodTypes
{
get { return this.foodTypes; }
set
{
this.foodTypes = value;
this.OnPropertyChanged("FoodTypes");
}
}
public MyViewModel()
{
this.FoodTypes = new ObservableCollection<FoodType>();
this.FoodTypes.Add(new FoodType() { Description = "Test1", Text = "Something" });
this.FoodTypes.Add(new FoodType() { Description = "Test1", Text = "Something Else" });
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
var handler = this.PropertyChanged;
if(handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}