我有一个包含 TourGuides 列表的 Booking,我正在尝试同时创建一个 Booking 和 TourGuides 列表,Booking 绑定正在工作,但 TourGuideOnTour 总是返回为空。
这是我的模型:
public class Booking : INotifyPropertyChanged {
public IList<TourGuide> TourGuidesOnTour {
get {
if (_tourGuidesOnTour == null)
return new List<TourGuide>();
return _tourGuidesOnTour;
}
set {
_tourGuidesOnTour = value;
OnPropertyChanged("TourGuidesOnTour");
}
}
}
public class TourGuide : INotifyPropertyChanged {
string _tourGuideFirstName;
public string TourGuideFirstName {
get { return _tourGuideFirstName; }
set {
_tourGuideFirstName = value;
OnPropertyChanged("TourGuideFirstName");
}
}
}
这是我的绑定:
<Grid Name="myGrid" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<StackPanel Margin="10">
<StackPanel.Resources>
<Style TargetType="{x:Type StackPanel}">
<Setter Property="Margin" Value="0,10,0,0"/>
</Style>
</StackPanel.Resources>
<GroupBox Header="Tour Information">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Tour" Width="130"/>
<TextBox Grid.Row="1" Grid.Column="1" Width="185">
<TextBox.Text>
<Binding Path="TourName" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<ExceptionValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</StackPanel>
</GroupBox>
<GroupBox Header="Tour Guide Information">
<DataGrid ItemsSource="{Binding Path=TourGuidesOnTour, Mode=TwoWay}">
</DataGrid>
</GroupBox>
<Button Name="Save" Content="Save" Click="Save_Click" Width="170" Height="40" />
</StackPanel>
</Grid>
我设置了我的数据上下文:
public Booking ReturnValue;// = null;
public CreateBooking() {
InitializeComponent();
ReturnValue = new Booking();
myGrid.DataContext = ReturnValue;
}
并且ReturnValue.TourGuidesOnTour
等于null
:(
谁能告诉我为什么?