Reputation: 1020
I'm new to WPF and I can't get my grid to auto-refresh when some property changes. Only thing I achieved - auto-refresh on element adding.
Here is my code:
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));
}
}
}
Upvotes: 0
Views: 6416
Reputation: 1020
The problem was here:
public string Name
{
get { return name; }
set
{
name = value;
RaisePropertyChanged(Name);
}
}
Had to use
RaisePropertyChanged("Name");
Simple and stupid..
Upvotes: 0
Reputation: 5691
Make you model variable notifyable and this will work for you.
Try Changing the above code with.
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>
Upvotes: 1