繁体   English   中英

为什么当我在 DataGrid 中做了一些更改时,我的 ObservableCollection 没有任何更改?

[英]Why I don't have any changes in my ObservableCollection when I have made some in my DataGrid?

我有一个 class 员工:人

Class 员工

 [Serializable]
    public enum Education
    {
        secondary, specialized_secondary, high
    }

    [Serializable]
    public enum MarriageStatus
    {
        single, married, divorced
    }

    [Serializable]
    public class Employee : Person, INotifyPropertyChanged
    {
        private Education _teaching;
        private MarriageStatus _status;
        private string _photoPath;

        public Education Teaching
        {
            get { return _teaching; }
            set
            {
                _teaching = value;
                OnPropertyChanged("Teaching");
            }
        }

        public MarriageStatus Status
        {
            get { return _status; }
            set
            {
                _status = value;
                OnPropertyChanged("Status");
            }
        }

        public string PhotoPath
        {
            get { return _photoPath; }
            set
            {
                _photoPath = value;
                OnPropertyChanged("Status");
            }
        }

        private ObservableCollection<Employee> _employeesList = null;

        public  new event PropertyChangedEventHandler PropertyChanged;

        public new void OnPropertyChanged([CallerMemberName]string prop = "")
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }

        public ObservableCollection<Employee> EmployeesList
        {
            get
            {
                if (_employeesList != null)
                {
                    return _employeesList;
                }
               
                    _employeesList = new ObservableCollection<Employee>();

                    _employeesList.Add(new Employee()
                    {
                        Id = 1,

                        FirstName = "Igor",
                        LastName = "Krivonos",
                        DateBirthday = new DateTime(1999, 8, 15),
                        INN = "111111111",
                        Teaching = Education.high,
                        Status = MarriageStatus.married,
                        PhotoPath = "Photo/IgorKrivonos.jpg"

                    });

                return _employeesList;
            }
        }

    }

我已经意识到 INotifyPropety 事件,但对此我不确定。 我是这个领域的新手,正在尝试一切可能有帮助的东西。 我所有的员工信息都显示在 DataGrid 中当我更改任何单元格时,它不会在调试器中显示任何更改。 我想序列化它以保存更改。 为什么我的更改不起作用? 对不起我的英语不好。

这是我的 xaml 代码:

Window x:Class="Employee_DataGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Employee_DataGrid"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:model="clr-namespace:Employee_DataGrid.Model"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <model:Employee x:Key="employees"></model:Employee>
        <ObjectDataProvider MethodName="GetValues"
                    ObjectType="{x:Type sys:Enum}"
                    x:Key="status">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="model:MarriageStatus" />
               
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        <ObjectDataProvider MethodName="GetValues"
                    ObjectType="{x:Type sys:Enum}"
                    x:Key="education">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="model:Education" />
               
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
       
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            
        </Grid.ColumnDefinitions>
        <!--Button for serialization-->
        <Button    Grid.Column="1" Click="Button_Click" >
            <TextBlock TextAlignment="Center" FontSize="35" FontFamily="TimesNewRoman" FontWeight="Bold" Width="30" TextWrapping="Wrap">Save  Data</TextBlock>
        </Button>
        <DataGrid AutoGenerateColumns="False" CanUserAddRows="False"
            ItemsSource="{Binding Source={StaticResource employees}, Path=EmployeesList}">
            <!--DataGrid Columns-->
            <DataGrid.Columns>
                <!--DataGridTextColumn for Full names and Inn-->
                <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"></DataGridTextColumn>
                <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"></DataGridTextColumn>
                <DataGridTextColumn Header="INN" Binding="{Binding INN}"></DataGridTextColumn>
                <!--DataGridComboBoxColumn for Marriage Status-->
                <DataGridComboBoxColumn Header="Status" 
                        ItemsSource="{Binding Source={StaticResource status}}" 
                        SelectedValueBinding="{Binding Status}" >

                </DataGridComboBoxColumn>
                <!--DataGridTemplateColumn for Birthday-->
                <DataGridTemplateColumn Header="Date Birthday">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <DatePicker  SelectedDate="{Binding DateBirthday, StringFormat='MM.dd.yyyy'}"></DatePicker>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <!--DataGridComboBoxColumn for Education-->
                <DataGridComboBoxColumn Header="Education"
                                        ItemsSource="{Binding Source={StaticResource education}}"
                                        SelectedValueBinding="{Binding Teaching}" >
                    
                </DataGridComboBoxColumn>
<!--DataGridTemplateColumn for Photos-->
                <DataGridTemplateColumn Header="Employees Photos">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image DockPanel.Dock="Right"
                               HorizontalAlignment="Right"
                               Width="70"
                               Source="{Binding Path=PhotoPath}"></Image>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>


            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Window>

我从人 Class 继承了一些属性这是我的人 Class:

[Serializable]
    public class Person: INotifyPropertyChanged
    {
        private int _id;
        private string _firstName;
        private string _lastName;
        private System.DateTime _dateBirthday;
        private string _inn;

        public int Id
        {
            get { return _id; }
            set
            {
                _id = value;
                OnPropertyChanged("Id");
            }
        }

         public string FirstName
        {
            get { return _firstName; }
            set
            {
                _firstName = value;
                OnPropertyChanged("FirstName");
            }
        }

         public string LastName
        {
            get { return _lastName; }
            set
            {
                _lastName = value;
                OnPropertyChanged("LastName");
            }
        }

         public System.DateTime DateBirthday
        {
            get { return _dateBirthday; }
            set
            {
                _dateBirthday = value;
                OnPropertyChanged("DateBirthday");
            }
        }

        public string INN
        {
            get { return _inn; }
            set
            {
                _inn = value;
                OnPropertyChanged("INN");
            }
        }


       
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName]string prop = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
        }

在这里,我也实现了 INotifyPropety。

主营Window class

[Serializable]
    public partial class MainWindow : Window
    {
      public  Employee Employee { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            Employee = new Employee();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Helpers.Serializing(Employee.EmployeesList, "employees.bin");
        }
    }

您正在创建Employee的两个实例,一个在代码隐藏中,另一个在 XAML 标记中。

您应该绑定到您在代码隐藏中创建的那个:

<DataGrid AutoGenerateColumns="False" CanUserAddRows="False"
        ItemsSource="{Binding Path=Employee.EmployeesList, 
            RelativeSource={RelativeSource AncestorType=Window}}">

...并删除它:

<model:Employee x:Key="employees"></model:Employee>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM