0

我是 WPF 新手,当某些属性发生变化时,我无法让我的网格自动刷新。我实现的唯一一件事 - 添加元素时自动刷新。

这是我的代码:

public partial class MainWindow : Window
{
    private Model model;

    public MainWindow()
    {
        InitializeComponent();
        model = new Model();
        MyGrid.ItemsSource = model.Content;
    }

    private void MyButton_Click(object sender, RoutedEventArgs e)
    {
        MyGrid.Items.Refresh();
    }
}

public class Model
{
    public ObservableCollection<Single> Content;
    private Random r;
    private Action action;
    private static object _syncLock = new object();

    public Model()
    {
        Content = new ObservableCollection<Single>();
        r = new Random(); 
        action = new Action(process);
        action.BeginInvoke(null,null);
        BindingOperations.EnableCollectionSynchronization(Content, _syncLock);          
    }

    private void process()
    {
        while (true)
        {
            Content.Add(new Single { Name = "name" });
            Content[r.Next(0,Content.Count())].Name = "rename" + r.Next(1,100);
            Thread.Sleep(1000);
        }
    }
}

public class Single : INotifyPropertyChanged
{
    private string name;
    public string Name 
    {
        get { return name; }
        set 
        {
            name = value;
            RaisePropertyChanged(Name);
        } 
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
4

2 回答 2

1

让您的模型变量可通知,这对您有用。

尝试更改上面的代码。

    public partial class Window1 : Window, INotifyPropertyChanged
{
    private Model model;

    public Model ModelData
    {
        get { return model; }
        set
        {
            model = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("ModelData"));
        }
    }

    public Window1()
    {
        this.InitializeComponent();
            ModelData = new Model();          
        MyGrid.ItemsSource = ModelData.Content;

    }

    public event PropertyChangedEventHandler PropertyChanged;
}
public class Model
{
    public ObservableCollection<Single> Content { get; set; }

    private Random r;

    private static object _syncLock = new object();

    public Model()
    {
        Content = new ObservableCollection<Single>();
        Content.Add(new Single { Name = "name" });
        r = new Random();

        // BindingOperations.EnableCollectionSynchronization(Content, _syncLock);
        DispatcherTimer t = new DispatcherTimer();
        t.Interval = new TimeSpan(2000);
        t.Tick += new EventHandler(t_Tick);
        t.Start();
    }

    void t_Tick(object sender, EventArgs e)
    {
        App.Current.Dispatcher.BeginInvoke(new Action(() =>
        {
            if (Content.Count <= 100)
                Content.Add(new Single { Name = "name" });
            Content[r.Next(0, Content.Count())].Name = "rename" + r.Next(1, 100);

        }));
    }


}
public class Single : INotifyPropertyChanged
{
    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            RaisePropertyChanged("Name");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Xaml

 <DataGrid Name="MyGrid"  AutoGenerateColumns="False"  Margin="246,175,0,0">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Names" Binding="{Binding Name }" />
            </DataGrid.Columns>
        </DataGrid>
于 2013-07-02T10:51:01.273 回答
0

问题出在这里:

public string Name
{
    get { return name; }
    set
    {
        name = value;
        RaisePropertyChanged(Name);
    }
}

不得不使用

RaisePropertyChanged("Name");

简单又笨。。

于 2013-07-02T13:08:18.730 回答