0

I have a DataGrid that has a selection of Jobs that users can add/update/delete. My problem with my DataGrid is that when the data is loaded into it, I want it to automatically fill in some TextBoxes with details of the first item in the DataGrid. I do this by writing the method for the SelectionChanged event on the 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;
            }
        }   

The issue here is that the TextBoxes (and DatePickers) are not filled in when the DataGrid loads. However, if I uncomment the MessageBox then the data is filled in correctly. This is a big problem as if there is only one job in the DataGrid then the user cannot select it to delete or update it. Here is the XAML for the DataGrid itself;

<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>

And the method that fills the 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;
            }
        }
    }

Is there something obvious I am missing that is preventing the SelectionChanged event from being called? I find it strange how if I uncomment the MessageBox it works as I intended.

CBreeze
  • 2,925
  • 4
  • 38
  • 93
  • Try putting `MessageBox.Show(selectedJob.ITName);` at the last line of method `DataGridSelectionChanged`. – user1672994 Dec 08 '15 at 11:07
  • @user1672994 if its on the last line then it doesn't load the data into the `TextBoxes`. The point here is that I don't want to have to display a message box just to get the data to bind to the `TextBoxes`. – CBreeze Dec 08 '15 at 11:27

1 Answers1

0

I've answered this question myself by completely removing the SelectionChanged event. Instead I bind the UI elements in the XAML like so;

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

Binding to the SelectedItem in XAML works correctly now.

CBreeze
  • 2,925
  • 4
  • 38
  • 93