Problem z DatePicker w DataGrid przy ObservableCollection.

0

Witam mam problem ze zmianą wartości Datepicker w DataGrid. Przy zmianie daty nie następuje żaden update ObservableCollection w Modelu.
Zauważyłem także że przy zmianie na przykład Imienia w DataGrid dopiero po kliknięciu w inny wiersz(row) zostanie dokonana zmiana w ObservableCollection.

XAML:

<DataGrid Margin="0 8 0 0" 
                      ItemsSource="{Binding Items}"
                      CanUserSortColumns="True"
                      CanUserAddRows="False"
                      AutoGenerateColumns="False"
                      materialDesign:DataGridAssist.CellPadding="13 8 8 8"
                      materialDesign:DataGridAssist.ColumnHeaderPadding="8"
                          VerticalScrollBarVisibility="Auto"
                          MaxHeight="400">

                <DataGrid.Columns>

                    <!-- Is Deleted -->
                    <DataGridCheckBoxColumn Binding="{Binding IsDeleted}"
                                                ElementStyle="{StaticResource MaterialDesignDataGridCheckBoxColumnStyle}"
                                                EditingElementStyle="{StaticResource MaterialDesignDataGridCheckBoxColumnEditingStyle}">
                        <DataGridCheckBoxColumn.Header>

                            <Border Background="Transparent"
                                        Padding="6 0 6 0"
                                        HorizontalAlignment="Center">
                                <CheckBox HorizontalAlignment="Center" 
                                              DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext}"
                                              IsChecked="{Binding IsDeleted}" />
                            </Border>

                        </DataGridCheckBoxColumn.Header>
                    </DataGridCheckBoxColumn>

                    <!-- Id -->
                    <DataGridTextColumn Binding="{Binding Id}"
                                            Header="Id"
                                            EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}" />

                    <!-- First Name -->
                    <materialDesign:MaterialDataGridTextColumn Binding="{Binding Firstname}"
                                                                   Header="Imię"
                                                                   EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnPopupEditingStyle}" />

                    <!-- End date Work -->
                    <DataGridTemplateColumn Header="Koniec umowy">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <DatePicker x:Name="LocaleDatePicker1"
                                                xml:lang="pl-PL"
                                                Width="120"
                                                materialDesign:HintAssist.Hint="Wybierz datę"
                                                SelectedDate="{Binding EndDateWork}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <!-- End date Work Permit -->
                    <DataGridTemplateColumn Header="Zezwolenie na pracę">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <DatePicker x:Name="LocaleDatePicker2"
                                                xml:lang="pl-PL"
                                                Width="120"
                                                materialDesign:HintAssist.Hint="Wybierz datę"
                                                SelectedDate="{Binding EndDateWorkPermit}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <!-- Edit Button -->
                    <DataGridTemplateColumn Header="Edycja">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Style="{StaticResource MaterialDesignRaisedDarkButton}"
                                        HorizontalAlignment="Center"
                                        Content="Zapisz"
                                        Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.UpdateEmployeeCommand}"
                                        CommandParameter="{Binding}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </materialDesign:Card>
        <Button Style="{StaticResource MaterialDesignRaisedDarkButton}"
                HorizontalAlignment="Center"
                Content="Zapisz"
                Margin="5"
                Command="{Binding EditEmployeeCommand}"
                />

screenshot-20180810192556.png

public class ListEmployeeViewModel : BaseViewModel
    {
        #region Public Properties

        /// <summary>
        /// Employee items to show
        /// </summary>
        public ObservableCollection<Employee> Items { get; set; }

        public string SearchText { get; set; }

        #endregion


        #region Commands

        public ICommand EditEmployeeCommand { get; set; }

        public ICommand FilterEmployeeCommand { get; set; }

        public ICommand UpdateEmployeeCommand { get; set; }

        #endregion


        #region Constructor

        public ListEmployeeViewModel()
        {
            ApplicationSettings.SetCultureInfo();
            EditEmployeeCommand = new RelayCommand(() => UpdateAllEmployees());
            UpdateEmployeeCommand = new RelayParameterizedCommand((p) => UpdateEmployee((Employee)p));
            FilterEmployeeCommand = new RelayCommand(() => FilterEmployee());
            Items = new ObservableCollection<Employee>();

            var employeeListResult = GetEmployeeList();

            foreach (var emp in employeeListResult)
            {
                Items.Add(emp);
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Update single employee
        /// </summary>
        /// <param name="emp">Employee to update</param>
        private void UpdateEmployee(Employee emp)
        {
            // Po zmianie daty w widoku nie zmieniła się data
            var empDataListAfterChangeDate = emp;
            return;
        }

        /// <summary>
        /// Update all employees
        /// </summary>
        private void UpdateAllEmployees()
        {
            // Po zmianie daty w widoku nie zmieniła się data
            var empDataListAfterChangeDate = Items;

            var result = EmployeesList.UpdateEmployeeList(Items.ToList());

            // Show confirm message when employee added successfully
            if (result == true)
            {
                MessageBox.Show("Edytowano pracowników");
            }
            // Otherwise show error message..
            else
                MessageBox.Show("Wystąpił błąd podczas edytowania pracowników", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }

        private List<Employee> GetEmployeeList()
        {
            return EmployeesList.GetEmployeeList();
        }

        private void FilterEmployee()
        {
            Items = new ObservableCollection<Employee>(EmployeesList.GetEmployeeFilteredList(SearchText));
        }

        #endregion

    }

PROPERTY CHANGED FODY

/// <summary>
    /// Base view model fires Property Changed event
    /// </summary>
    public class BaseViewModel : INotifyPropertyChanged
    {
        /// <summary>
        /// The event that is fired when property value is changed
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };

        /// <summary>
        /// Call this to fire a <see cref="PropertyChanged"/> event
        /// </summary>
        /// <param name="name"></param>
        public void OnPropertyChanged(string name)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}

Przed zmianą daty
screenshot-20180810193550.png
Po zmianie daty
screenshot-20180810193616.png
Pojedynczy update pracownika
screenshot-20180810193735.png
Update wszystkich pracowników
screenshot-20180810194102.png

0

Poszukaj informacji o "Binding modes" i INotifyPropertyChanged.

0

Zmiany zostają zapisane po kliknięciu w inny wiersz bo wtedy następuje koniec edycji. Tak samo powinno zadziałać kliknięcie entera. Co do DatePicker to walczę dokładnie z tym samym problemem :(

1 użytkowników online, w tym zalogowanych: 0, gości: 1