我有一个ListBox,它通过ViewModel和DataTemplate填充,我也有一个绑定。问题是DataTemplate中的绑定不起作用。这是我的代码:
视图模型:
private void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
private string strHeaderText;
public string HeaderText
{
get
{
if (null != this.Trips)
{
if (null != this._trips.name)
{
return this._trips.name;
}
}
return "Trip";
}
set
{
this.strHeaderText = value;
}
}
public TripTypeViewModel()
{
this.TripTypeViewModelDataSource.Clear();
foreach (TripType tt in TripsResponseTypeViewModelDataSource.FirstOrDefault().Trip)
{
this.TripTypeViewModelDataSource.Add(tt);
}
}
...
private ObservableCollection<TripType> tripTypeViewModelDataSource;
public ObservableCollection<TripType> TripTypeViewModelDataSource
{
get
{
if (null == this.tripTypeViewModelDataSource)
{
this.tripTypeViewModelDataSource = new ObservableCollection<TripType>();
}
return this.tripTypeViewModelDataSource;
}
}
这是我的查看
<ListBox x:Name="ItemsLB" ItemsSource="{Binding tripTypeViewModel.TripTypeViewModelDataSource}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Content="{Binding tripTypeViewModel.HeaderText}" Width="Auto" Height="Auto"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>