我正在使用MVVM开发一个WPF应用程序,我试图显示并添加一个作业标题(基本上是一个字符串)。所以,在" index"查看我将显示SQL Server数据库中的所有列表,在列表的顶部有一个"添加新的职位标题"这将打开一个带有文本框和按钮的新窗口。
// this is my collection that holds the job titles
public ObservableCollection<JobTitle> JobTitles
{
get { return _jobTitles; }
set
{
if (_jobTitles == value) return;
_jobTitles = value;
OnPropertyChanged();
}
}
JobTitles = GetJobTitles();
// opens up a new window
private void AddNewJobTitleCommandHandler()
{
var createJobTitleWindow = new CreateJobTitle()
{
ShowInTaskbar = false,
Owner = Application.Current.MainWindow,
Title = "Add New Job Title"
};
createJobTitleWindow.ShowDialog();
}
从&#34;添加新职位&#34;窗口我用这个:
private void AddJobTitleCommandHandler()
{
if (JobTitleInput == null) return;
var jt = new JobTitle { Description = JobTitleInput };
_dbContext.JobTitles.Add(jt);
_dbContext.SaveChanges();
var windows = Application.Current.Windows;
// closes the last opened window ?? better way to do
windows[windows.Count - 1].Close();
windows[0].Focus();
JobTitles = GetJobTitles();
}
虽然我在关闭窗口后更新了JobTitles
但是我没有刷新列表框我用来显示JobTitles
<ListBox ItemsSource="{Binding JobTitles}" Grid.Row="2">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" Name="Description" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Description}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
如何在关闭子窗口后更新我的父页面?
UPDATE 好的,我这样做是有效的:
windows[windows.Count - 1].DialogResult = true; // in AddJobTitleCommandHandler
在AddNewJobTitleCommandHandler
中if (createJobTitleWindow.ShowDialog() == true)
{
// update after create window is closed
JobTitles = GetJobTitles();
};