动态数据网格视图更新

时间:2018-06-05 10:06:39

标签: c# wpf dynamic datagrid inotifypropertychanged

嘿大家我是WPF的新手,试图学习一些东西。 在dataGrid中,当我更新dataList时,我的网格没有得到更新 我的XAML是:

<Window x:Class="DataGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib" 
        xmlns:l="clr-namespace:DataGrid" Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ObjectDataProvider x:Key="selectionUnits" 
                        MethodName="GetNames" ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="DataGridSelectionUnit" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        <ObjectDataProvider x:Key="selectionModes" 
                        MethodName="GetNames" ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="DataGridSelectionMode" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        <ObjectDataProvider x:Key="headersVisibility" 
                        MethodName="GetNames" ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="DataGridHeadersVisibility" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        <l:GenderTemplateSelector x:Key="genderTemplateSelector">
            <l:GenderTemplateSelector.MaleTemplate>
                <DataTemplate>
                    <Grid Background="LightBlue">
                        <Image Source="{Binding Image}" Width="50" />
                    </Grid>
                </DataTemplate>
            </l:GenderTemplateSelector.MaleTemplate>
            <l:GenderTemplateSelector.FemaleTemplate>
                <DataTemplate>
                    <Grid Background="Salmon">
                        <Image Source="{Binding Image}" Width="50" />
                    </Grid>
                </DataTemplate>
            </l:GenderTemplateSelector.FemaleTemplate>
        </l:GenderTemplateSelector>
    </Window.Resources>
    <Grid>
        <TabControl TabStripPlacement="Left">
            <TabItem Header="Auto Column Generation">
                <DataGrid ItemsSource="{Binding Customers}" />
            </TabItem>

        </TabControl>
    </Grid>
</Window>

我的xaml.cs文件是:

public partial class MainWindow : Window
{
    public MainWindow()
    {
       InitializeComponent();
       DataContext = new MainWindowViewModel();
    }
}

 public class MainWindowViewModel
    {
        public ICollectionView Customers { get; private set; }
        public ICollectionView GroupedCustomers { get; private set; }


    public MainWindowViewModel()
    {
        var timer = new System.Timers.Timer();

        timer.Elapsed += timer_Tick;
        timer.Interval = 20000;
        timer.Enabled = true;
        timer.Start();
        AddServices();



    }
    void timer_Tick(object sender, EventArgs e)
    {
        AddServicesTimer();
    }
    public void AddServicesTimer()
    {

        var _customers = new List<Customer>();
        for(int i = 0; i < 5; i++)
        {
           var c = new Customer
            {
                FirstName = "Christian",
                Name = "SrvNameTimer",
                State = "SrvState",
                LastName = "Moser",
                Gender = Gender.Male,
                WebSite = new Uri("http://www.wpftutorial.net"),
                ReceiveNewsletter = true,
                Image = "Images/christian.jpg"
            };
            _customers.Add(c);
        }

        Customers = CollectionViewSource.GetDefaultView(_customers);

        GroupedCustomers = new ListCollectionView(_customers);

    }
    public void AddServices()
    {

        var _customers = new List<Customer>();
        for (int i = 0; i < 5; i++)
        {
            var c = new Customer
            {
                FirstName = "Christian",
                Name = "SrvName",
                State = "SrvState",
                LastName = "Moser",
                Gender = Gender.Male,
                WebSite = new Uri("http://www.wpftutorial.net"),
                ReceiveNewsletter = true,
                Image = "Images/christian.jpg"
            };
            _customers.Add(c);
        }

        Customers = CollectionViewSource.GetDefaultView(_customers);

        GroupedCustomers = new ListCollectionView(_customers);

    }
}

客户类定义:

public class Customer : INotifyPropertyChanged
    {
        private string _firstName;
        private string _State;
        private string _Name;
        private string _lastName;
        private Gender _gender;
        private Uri _webSite;
        private bool _newsletter;
        private string _image;
    public string FirstName
    {
        get { return _firstName; }
        set 
        {
            _firstName = value;
            NotifyPropertyChanged("FirstName");
        }
    }
    public string State
    {
        get { return _State; }
        set
        {
            _State= value;
            NotifyPropertyChanged("State");
        }
    }
    public string Name
    {
        get { return _Name; }
        set
        {
            _Name = value;
            NotifyPropertyChanged("Name");
        }
    }

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

    public Gender Gender
    {
        get { return _gender; }
        set
        {
            _gender = value;
            NotifyPropertyChanged("Gender");
        }
    }

    public Uri WebSite
    {
        get { return _webSite; }
        set
        {
            _webSite = value;
            NotifyPropertyChanged("WebSite");
        }
    }

    public bool ReceiveNewsletter
    {
        get { return _newsletter; }
        set
        {
            _newsletter = value;
            NotifyPropertyChanged("Newsletter");
        }
    }

    public string Image
    {
        get { return _image; }
        set
        {
            _image = value;
            NotifyPropertyChanged("Image");
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    #region Private Helpers

    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

在开始时调用AddServices()方法。 20秒后,我调用另一个名为AddServicesTimer()的方法 但它没有更新我的网格视图它正在更新列表而不是更新视图。 需要帮助

0 个答案:

没有答案