I have a simple listview, with a grid control. I have bound an object to the grid using observablecollection. inotifyPropertyChanged is implemented on the setter.
The grid has three columns. On a button click I load the grid with some rows of data in two columns. Then the user clicks on another button, and i add some text in the third column in the grid too. Problem is that this new text gets displayed in the grid on only those rows which are not currently on screen. If I scroll down and up, the rest get loaded too as soon as they leave the screen area beyond the scroll.
This might be a starter question, but I have tried various permutations of my code, and have searched and read articles but none helped.
XAML
<Window x:Class="MediaFolderCleanupTool.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="392" Width="582">
<Grid Height="340" Width="550">
<Button Content="Search" Height="23" HorizontalAlignment="Left" Margin="12,38,0,0" Name="btnSearch" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<ListView ItemTemplate="{Binding FileItem}" Height="198" HorizontalAlignment="Left" Margin="14,67,0,0" Name="lstTargetFiles" VerticalAlignment="Top" Width="524" >
<ListView.View>
<GridView>
<GridViewColumn Header="" Width="40">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected}" Name="Select" IsThreeState="False" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="File"
Width="350"
DisplayMemberBinding="{Binding Path=Name}" />
<GridViewColumn Header="Status"
Width="100"
DisplayMemberBinding="{Binding Path=Status}" />
</GridView>
</ListView.View>
</ListView>
<Button Content="Delete" Height="23" HorizontalAlignment="Left" IsEnabled="False" Margin="13,306,0,0" Name="btnDelete" VerticalAlignment="Top" Width="75" Click="btnDelete_Click_1" />
<Label Height="28" HorizontalAlignment="Left" Margin="16,271,0,0" Name="lblCount" VerticalAlignment="Top" Width="371" />
</Grid>
</Window>
On button click on screen, the below gets called
private void DelFiles(ObservableCollection<FileItem> files)
{
foreach (FileItem fi in files)
{
try
{
fi.Status = "Deleted";
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
fi.Status = "Error Deleting";
}
}
}
And here is the FileItem class
class FileItem : INotifyPropertyChanged
{
public const string NamePropertyName = "CheckBoxState";
private bool _checkboxstate = true;
public string Name { get; set; }
public string Status { get; set; }
public bool IsSelected
{
get
{
return _checkboxstate;
}
set
{
if (_checkboxstate == value)
{
return;
}
_checkboxstate = value;
OnPropertyChanged(NamePropertyName);
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Try this: