tengo la siguiente prueba de concepto:¿Es esto un error de WPF Datagrid?
ventana XAML:
<Window x:Class="WpfApplication1.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">
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" />
<DataGridTemplateColumn >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Mode=TwoWay, Path=Enabled}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Window>
Código atrás:
using System.Collections.ObjectModel;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public ObservableCollection<Data> Items { get; private set; }
public MainWindow()
{
InitializeComponent();
this.Items = new ObservableCollection<Data>();
this.DataContext = this;
for (int index = 0; index < 30; index++)
{
this.Items.Add(new Data() {Enabled = true });
}
}
}
public class Data
{
public bool Enabled { get; set; }
}
}
Ejecutar la aplicación, desactive algunas cajas en la parte superior, desplácese hacia abajo, cambiar algunos cajas de nuevo y desplazarse hacia arriba. Voilá, ¡las casillas de verificación se revisan nuevamente!
¿Me falta algo o debo completar un error en Microsoft?
EDIT: Gracias por sus respuestas, pero no está relacionado con el INotify o la casilla de verificación, con un TextBox y el INotify pasa lo mismo. No es necesario que haga clic en las casillas de verificación después de desplazarse, simplemente desmarque algunos, desplácese hacia abajo, desplácese hacia arriba y listo, se verifican de nuevo. Marque este código:
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTemplateColumn >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Mode=TwoWay, Path=Enabled}" />
<TextBox Text="{Binding Text}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Window>
Y el código subyacente:
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public ObservableCollection<Data> Items { get; private set; }
public MainWindow()
{
InitializeComponent();
this.Items = new ObservableCollection<Data>();
this.DataContext = this;
for (int index = 0; index < 30; index++)
{
this.Items.Add(new Data() { Enabled = true, Text = index.ToString() });
}
}
}
public class Data : INotifyPropertyChanged
{
private bool _enabled;
public bool Enabled
{
get { return _enabled; }
set
{
if (value != _enabled)
{
_enabled = value;
this.OnPropertyChanged("Enabled");
}
}
}
private string _text;
public string Text
{
get { return _text; }
set
{
if (value != _text)
{
_text = value;
this.OnPropertyChanged("Text");
}
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string name)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
#endregion
}
}
¿Su resultado cambia si hace que 'Data' herede de' INotifyPropertyChanged' y usa las notificaciones de cambio de propiedad? – Rachel
Intente desactivar el reciclaje. ¿El conjunto se llama? Si no es UpdateSourceTrigger = "PropertyChanged". Y INotify como Rachel dijo. – Paparazzi
Probaría lo que sugirió Rachel. – ecMode