我不知道该怎么做。我已经检查了代码以及它看起来很好的一切。
所以我的问题是组合框没有填充任何数据,我已经检查了ObservableCollection集并且它有数据,但它不是任何人都用的
我使用MVVM模式,一切顺利,但GET不被称为
我可以得到一些提示可能是什么原因,因为我没有想法
StudentDetailView.xaml
<Grid x:Name="LayoutRoot" Background="White">
<Grid DataContext="{Binding}" HorizontalAlignment="Left" Margin="12,12,0,0" Name="grid1" VerticalAlignment="Top" Height="176" Width="376">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="31*" />
</Grid.RowDefinitions>
<sdk:Label Content="Clasa ID:" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<ComboBox Grid.Column="1" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="3"
Name="clasaIDComboBox" VerticalAlignment="Center" Width="120"
DisplayMemberPath="Nume"
SelectedValuePath="ID" />
<sdk:Label Content="Data Nasterii:" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<sdk:DatePicker Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="3" Name="data_NasteriiDatePicker" VerticalAlignment="Center" Width="120" />
<sdk:Label Content="ID:" Grid.Column="0" Grid.Row="2" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="3" Name="iDTextBox" VerticalAlignment="Center" Width="120" />
<sdk:Label Content="Liceu ID:" Grid.Column="0" Grid.Row="3" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Grid.Row="3" Height="23" HorizontalAlignment="Left" Margin="3" Name="liceuIDTextBox" VerticalAlignment="Center" Width="120" />
<sdk:Label Content="Nume:" Grid.Column="0" Grid.Row="4" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
<TextBox Grid.Column="1" Grid.Row="4" Height="23" HorizontalAlignment="Left" Margin="3" Name="numeTextBox" VerticalAlignment="Center" Width="120" />
</Grid>
<Button Content="Save Student" Height="23" HorizontalAlignment="Left" Margin="194,232,0,0" Name="btnSaveStudent" VerticalAlignment="Top" Width="95" Click="btnSaveStudent_Click" />
<Button Content="Cancel" Height="23" HorizontalAlignment="Left" Margin="295,232,0,0" Name="btnCancelStudent" VerticalAlignment="Top" Width="75" Click="btnCancelStudent_Click" />
<ct:ValidationSummary Width="300" Margin="12,194,70,23" />
</Grid>
StudentDetailView.xaml.cs
public StudentDetailView()
{
InitializeComponent();
clasaIDComboBox.SetBinding(ComboBox.ItemsSourceProperty, new Binding() { Path = new PropertyPath("LstHighSchool"), Mode = BindingMode.TwoWay });
clasaIDComboBox.SetBinding(ComboBox.SelectedValueProperty, new Binding() { Path = new PropertyPath("SelectedHighSchool"), Mode = BindingMode.TwoWay, NotifyOnValidationError = true, ValidatesOnExceptions = true });
}
来自StudentViewModel.cs的部分代码
private void AddNewStudentView(object param)
{
StudentDetailView addNewStudentView = new StudentDetailView();
addNewStudentView.DataContext = new Student();
var sdfsdfj = new StudentViewModel();
LoadOperation<Liceu> li = DomainContext.Load(DomainContext.GetLiceusQuery());
li.Completed += (s, e) =>
{
LstHighSchool = (s as LoadOperation<Liceu>).Entities.ToObservableCollection();
};
addNewStudentView.Closed += delegate
{
if (addNewStudentView.DialogResult == true)
{
Student newStudent = addNewStudentView.DataContext as Student;
if (string.IsNullOrEmpty(newStudent.Nume))
{
newStudent.ValidationErrors.Add(new ValidationResult("Numele nu poate sa fie invalid",new string[]{"Nume"}));
}
if (!newStudent.LiceuID.HasValue)
{
newStudent.ValidationErrors.Add(new ValidationResult("Liceu ID este gol", new string[] { "LiceuID" }));
}
if (!newStudent.HasValidationErrors)
{
if (newStudent != null)
{
DomainContext.Students.Add(newStudent);
LstStudents.Add(newStudent);
}
}
else
{
addNewStudentView.Show();
}
}
};
addNewStudentView.Show();
OnPropertyChanged("LstHighSchool");
}
private bool CanWork(object param)
{
return true; //for the moment this remains true, cause you can add all the time a new student, but this can be changed if there must be some conditions
}
private ObservableCollection<Liceu> _lstHighSchool;
public ObservableCollection<Liceu> LstHighSchool
{
get
{
return _lstHighSchool;
}
set
{
_lstHighSchool = value;
OnPropertyChanged("LstHighSchool");
}
}
private int _selectedHighSchool;
public int SelectedHighSchool
{
get
{
return _selectedHighSchool;
}
set
{
_selectedHighSchool = value;
OnPropertyChanged("SelectedHighSchool");
}
}
#endregion
答案 0 :(得分:1)
首先,摆脱Grid DataContext="{Binding}"
,然后xaml中的组合框应如下所示:
<ComboBox Grid.Column="1" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="3"
VerticalAlignment="Center" Width="120" DisplayMemberPath="Nume"
SelectedValuePath="ID" ItemsSource="{Binding LstHighSchool}"
SelectedValue="{Binding SelectedHighSchool}" />
其次,View的代码背后应该是,
public StudentDetailView()
{
InitializeComponent();
this.DataContext= new StudentViewModel();
}
最后,在StudentViewModel.cs中应该有一个名为LstHighSchool
的属性看起来很好。