グリッドコントロールを備えた単純なリストビューがあります。observablecollection を使用してオブジェクトをグリッドにバインドしました。inotifyPropertyChanged はセッターに実装されています。
グリッドには 3 つの列があります。ボタンをクリックすると、2 つの列にいくつかの行のデータがグリッドに読み込まれます。次に、ユーザーが別のボタンをクリックすると、グリッドの 3 番目の列にもテキストが追加されます。問題は、この新しいテキストが、現在画面に表示されていない行のグリッドにのみ表示されることです。上下にスクロールすると、スクロールを超えて画面領域を離れるとすぐに残りも読み込まれます。
これは初心者の質問かもしれませんが、コードのさまざまな順列を試し、記事を検索して読みましたが、何も役に立ちませんでした。
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));
}
}