我有一個預訂,其中有一個TourGuides的列表,我試圖創建一個預訂和TourGuides的列表同時,預訂綁定工作,但TourGuideOnTour是總是回來爲空。WPF綁定多對一的關係返回空結果
繼承人我的模型:
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>
設置我的DataContext:
public Booking ReturnValue;// = null;
public CreateBooking() {
InitializeComponent();
ReturnValue = new Booking();
myGrid.DataContext = ReturnValue;
}
和ReturnValue.TourGuidesOnTour
等於null
:(
有誰能告訴我爲什麼?
冉代碼,只是刪除按鈕,並添加一個項目的清單,這一切都表現得很好。我總是遇到自動生成的膠捲出現問題,所以我不會指望它。 –