我有两个带有集合的ViewModel,并将它们分别设置为ItemsSource到一个Pivot和一个ListBox。问题是我的ListBox在Pivot的DataTemplate中没有任何反应? ListBox没有填充..还有PivotItem.Header不起作用我的XAML出了什么问题?
我的页面前端:
<Pivot ItemsSource="{Binding tripTypeViewModel.TripTypeViewModelDataSource}" x:Name="TripsSegmentsPivot" Title=" " Foreground="#FF888888" Style="{StaticResource PivotStyle1}" SelectionChanged="Pivot_SelectionChanged" Margin="0" Grid.Row="1">
<Pivot.ItemTemplate>
<DataTemplate>
<PivotItem Margin="8,8,8,0" Background="Red">
<PivotItem.Header>
<TextBlock Text="{Binding tripTypeViewModel.HeaderText}" Margin="0, 16, 0, 0" Foreground="#FF888888" FontSize="32" FontFamily="Segoe WP" FontWeight="Light"/>
</PivotItem.Header>
<ListBox x:Name="ItemsLB" ItemsSource="{Binding tripTypeViewModel.Segment}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="DarkKhaki">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Content="test" Width="Auto" Height="Auto" HorizontalAlignment="Stretch"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</PivotItem>
</DataTemplate>
</Pivot.ItemTemplate>
</Pivot>
这是我的ViewModel:
public class TripTypeViewModel:TripsResponseTypeViewModel,INotifyPropertyChanged { #region改变了 公共事件PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
private ObservableCollection<TripTypeSegment> segmentField;
public ObservableCollection<TripTypeSegment> Segment
{
get
{
if (null != this.Trips)
{
return this.segmentField;
}
return new ObservableCollection<TripTypeSegment>();
}
set
{
this.segmentField = value;
}
}
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;
}
}
这是我的ViewModel的Master类:
class CompositeViewModel
{
public static TripsResponseTypeViewModel tripsResponseTypeViewModel { get; set; }
public static TripTypeViewModel tripTypeViewModel { get; set; }
static CompositeViewModel()
{
tripsResponseTypeViewModel = new TripsResponseTypeViewModel();
tripTypeViewModel = new TripTypeViewModel();
}
}
我的观点代码隐藏:
public MyTripsPage()
{
this.InitializeComponent();
this.DataContext = new CompositeViewModel();
}
不仅没有填充ListBox,还没有设置pivot项的标题?那是为什么?