我有一个简单的列表视图,带有一个网格控件。我已经使用 observablecollection 将对象绑定到网格。inotifyPropertyChanged 在 setter 上实现。
网格有三列。在一个按钮上单击我加载了两列中包含一些数据行的网格。然后用户单击另一个按钮,我也在网格的第三列中添加了一些文本。问题是这个新文本只显示在网格中当前不在屏幕上的那些行上。如果我向下和向上滚动,其余的也会在它们离开滚动区域之外的屏幕区域时立即加载。
这可能是一个入门问题,但我已经尝试了我的代码的各种排列,并且搜索和阅读了文章但没有任何帮助。
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>
在屏幕上单击按钮,调用以下内容
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";
}
}
}
这是 FileItem 类
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));
}
}