0

私は動的データベースを持っており、データベース内のデータは1分以内に更新されます。ここで、WPFプロジェクトでデータグリッドを作成し、データベースのすべてのデータを表示したいと思います。プロジェクトを実行すると、データグリッドには静的データ(プロジェクト実行前のデータ)のみが表示されます。データグリッドを実行した後も、データグリッドが自動的に更新されるようにするにはどうすればよいですか?ところで、私はWPFプロジェクトにlinq to sqlとC#を使用しています。

データグリッドのコードスニペット:

<DataGrid AutoGenerateColumns="False" Name="MyDataGrid" VerticalAlignment="Top" Width="185" >
     <DataGrid.Columns>
          <DataGridTextColumn Header="Date"  Width="60" Binding="{Binding Date}" />
          <DataGridTextColumn Header="Time" Width="55" Binding="{Binding Time}"/>
          <DataGridTextColumn Header="Id" Width="69" Binding="{Binding id}" />
    </DataGrid.Columns>
</DataGrid>

コードビハインドのコードスニペット:

public  MainWindow()
{
        InitializeComponent();

        using (MyDummyDataContext db = new MyDummyDataContext())
        {
            var query = from p in db.Ids
                        orderby p.Id descending
                        select new 
                        { 
                            Date = p.Date,
                            Time = p.Time,
                            id = p.Id
                        };
            MyDataGrid.ItemsSource = query;
        }
}
4

1 に答える 1

1

ここに私の2セントがあります:

  1. DataGird の ItemsSource を ObservableCollection にバインドします。
  2. 読み込まれたイベント ハンドラーでコレクションを初期化します。
  3. タイマーを追加すると、タイマーのコールバックで、データベースからコレクションを更新できます。注:.NET 4.5 を使用している場合、ObservableCollection フォーム バックグラウンド スレッドの更新がサポートされています。それ以外の場合は、スレッドの同期を手動で処理する必要があります。

バックグラウンドでデータを更新するためのリンクは次のとおりです。問題に完全に適合しない場合がありますが、いくつかのアイデアを得ることができます。

Observable Collection クロススレッド変更通知

編集:

例を書きました (簡単にするために、UI スレッドでコレクションを更新する DispatcherTimer を使用します。バックグラウンド スレッドでデータを更新するには、代わりに System.Timers.Timer を使用し、リンクのメソッドを使用する必要があります。):

アプリ.xaml.cs:

using System.Windows;

namespace DataGridTest
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        var vm = new MainWindowViewModel();
        var mainWindow = new MainWindow { DataContext = vm };
        mainWindow.Show();
    }
}
}

MainWindow.xaml

<Window x:Class="DataGridTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <DataGrid AutoGenerateColumns="False" Name="MyDataGrid" VerticalAlignment="Top" Width="185"
              ItemsSource="{Binding Persons}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Id"  Width="60" Binding="{Binding Id}" />
            <DataGridTextColumn Header="Name" Width="55" Binding="{Binding Name}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

MainWindow.cs:

using System.Windows;

namespace DataGridTest
{
using System;
using System.ComponentModel;
using System.Windows.Threading;

public class Person : INotifyPropertyChanged
{
    private int _id;

    private string _name;

    public int Id
    {
        get
        {
            return _id;
        }

        set
        {
            if (this._id == value)
                return;

            this._id = value;
            this.OnPropertyChanged("Id");
        }
    }
    public string Name
    {
        get
        {
            return _name;
        }

        set
        {
            if (this._name == value)
                return;

            this._name = value;
            this.OnPropertyChanged("Name");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private readonly DispatcherTimer _timer = new DispatcherTimer();

    public MainWindow()
    {
        InitializeComponent();

        _timer.Interval = TimeSpan.FromSeconds(1);
        _timer.Tick += this._timer_Tick;
        _timer.Start();
    }

    private void _timer_Tick(object sender, EventArgs e)
    {
        var vm = this.DataContext as MainWindowViewModel;
        if(vm != null)
            vm.Refresh();
    }
}
}

MainWindowViewModel.cs

namespace DataGridTest
{
  using System.Collections.ObjectModel;
  using System.ComponentModel;

public class MainWindowViewModel : INotifyPropertyChanged
{
    private readonly ObservableCollection<Person> _persons = new ObservableCollection<Person>();

    private static int _id = 3;

    public ObservableCollection<Person> Persons
    {
        get
        {
            return _persons;
        }
    }

    public MainWindowViewModel()
    {
        _persons.Add(new Person { Id = 1, Name = "A" });
        _persons.Add(new Person { Id = 2, Name = "B" });
        _persons.Add(new Person { Id = 3, Name = "C" });  
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public void Refresh()
    {
        _persons.Add(new Person() { Id = ++_id, Name = _id.ToString() });
    }

}

}

于 2012-11-27T08:24:50.487 に答える