SelectedItem未在DataGrid上加载

时间:2015-12-08 10:49:59

标签: c# wpf xaml datagrid

我有一个DataGrid,其中有Jobs个用户可以添加/更新/删除的选项。我的问题DataGrid的问题在于,当数据加载到其中时,我希望它自动填写一些TextBoxes,其中包含DataGrid中第一项的详细信息。我是通过在SelectionChanged;

上编写DataGrid事件的方法来完成的
    private void DataGridSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedJob = dataGrid.SelectedItem as JobModel;
            //MessageBox.Show(selectedJob.ITName);
            if (selectedJob != null)
            {
                notesTextBox.Text = selectedJob.CaseNotes;
                createdPicker.SelectedDate = DateTime.Parse(selectedJob.DateCreated.ToString("yyyy-MM-dd"));
                deadlinePicker.SelectedDate = DateTime.Parse(selectedJob.DateDeadline.ToString("yyyy-MM-dd"));
                nameTextBox.Text = selectedJob.CaseClient;
                itNameComboBox.SelectedItem = selectedJob.ITName;
            }
        }   

此处的问题是,TextBoxes加载后DatePickers(和DataGrid)未填写。但是,如果我取消注释MessageBox,则数据会正确填写。这是一个很大的问题,好像DataGrid中只有一个作业,然后用户无法选择它来删除或更新它。以下是XAML本身的DataGrid;

<DataGrid x:Name="dataGrid" ColumnWidth="*" IsReadOnly="true" 
                      Margin="10" Grid.ColumnSpan="3" FontSize="18.667" 
                      HeadersVisibility="Column" AutoGenerateColumns="False" 
                      CanUserAddRows="False"
                      IsSynchronizedWithCurrentItem="True"
                      EnableRowVirtualization="True"
                      ItemsSource="{Binding JobsCollectionView}"
                      MinColumnWidth="0" SelectionChanged="DataGridSelectionChanged">
                <DataGrid.RowStyle>
                    <Style TargetType="DataGridRow">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Overdue}" Value="1">
                                <Setter Property="Background" Value="Red"/>
                                <Setter Property="FontWeight" Value="Bold"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.RowStyle>
                <DataGrid.Columns>
                    <!--<DataGridTextColumn Header="Case NO" Width="*" Binding="{Binding CaseNumber}"/>!-->
                    <DataGridTextColumn Header="IT Person" Width="*" Binding="{Binding ITName}"/>
                    <DataGridTextColumn Header="Case Notes" Width="*" Binding="{Binding CaseNotes}"/>
                    <DataGridTextColumn Header="Date Created" Width="*" Binding="{Binding DateCreated, StringFormat=dd/MM/yyyy}"/>
                    <DataGridTextColumn Header="Deadline Date" Width="*" Binding="{Binding DateDeadline, StringFormat=dd/MM/yyyy}"/>
                    <DataGridTextColumn Header="Employee Name" Width="*" Binding="{Binding CaseClient}"/>
                </DataGrid.Columns>
            </DataGrid>

填充DataGrid;

的方法
    public void FillDataGrid()
    {
        _jobDataService = new JobDataService();
        var _jobs = new ObservableCollection<JobModel>();
        _jobs = _jobDataService.GetJobList();
        JobsCollectionView = CollectionViewSource.GetDefaultView(_jobs);
        DataContext = this;
        foreach (JobModel currentJob in JobsCollectionView)
        {
            if (DateTime.Now == currentJob.DateDeadline)
            {
                currentJob.Overdue = 0;
            }
            else if (DateTime.Now > currentJob.DateDeadline)
            {
                currentJob.Overdue = 1;
            }
        }
    }

是否有一些明显我遗漏的东西阻止了SelectionChanged事件被调用?如果我取消MessageBox它按预期工作的话,我觉得很奇怪。

1 个答案:

答案 0 :(得分:0)

我已经通过完全删除SelectionChanged事件来回答这个问题。相反,我将XAML中的UI元素绑定在一起;

<TextBox x:Name="notesTextBox" Text="{Binding SelectedItem.CaseNotes, ElementName=dataGrid}" Margin="5" FontSize="21.333" Grid.Column="1" VerticalAlignment="Center"/>

绑定到SelectedItem中的XAML现在可以正常工作。