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