gpt4 book ai didi

c# - 从另一个类更新时更新 ListView

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:16 24 4
gpt4 key购买 nike

我正在构建一个简单的应用程序,它在一个 ListView 中显示特定格式的所有文件。我已将程序划分为“DataFiles”类,如果那里发生变化,我会在其中建立 FileSystemWatcher。如果有什么变化,那么我想更新我的 Listview,它位于 MainWindows.xaml 中。

所以我在 MainWindow.xaml 中有我的 MainWindowViewModel.cs 和 DataFiles.cs 和 ListView。如何将列表从 DataFiles 更新到 MainWindow.xaml?

如果有帮助,我的 DataFiles 类:

public FileSystemWatcher filewatcher;
public string ConfigurationFilesSourcePath;
public ObservableCollection<Files> fileslist { get; protected set; } = new ObservableCollection<Files>();

public void InitializeFiles()
{
// Create a new FileSystemWatcher
filewatcher = new FileSystemWatcher();

// Set filter
filewatcher.Filter = "*.txt";

// Set the path
filewatcher.Path = ConfigurationFilesSourcePath;

// Subscribe to the Created event
filewatcher.Created += new FileSystemEventHandler(FileOnchanged);
filewatcher.Changed += new FileSystemEventHandler(FileOnchanged);
filewatcher.Deleted += new FileSystemEventHandler(FileOnchanged);
filewatcher.Renamed += new RenamedEventHandler(FileOnRenamed);

// Enable the FileSystemWatcher events
filewatcher.EnableRaisingEvents = true;

RefreshFilesList();
}

private void FileOnchanged(object sender, FileSystemEventArgs e)
{
RefreshFilesList();
}

private void FileOnRenamed(object sender, RenamedEventArgs e)
{
RefreshFilesList();
}

public void RefreshFilesList()
{
fileslist.Clear();

//string[] getfiles = Directory.GetFiles(FolderLocation);
DirectoryInfo dir = new DirectoryInfo(ConfigurationFilesSourcePath);

string[] extensions = new[] { ".txt" };

int nof = 0;
foreach (FileInfo file in dir.GetFiles().Where(f => extensions.Contains(f.Extension.ToLower())).ToArray())
{
nof++;

fileslist.Add(new Files()
{
FileId = nof,
FileName = file.Name,
FileChanged = file.LastWriteTime.ToString(),
FileCreated = file.CreationTime.ToString(),
OnlyNameWithoutExtension = Path.GetFileNameWithoutExtension(file.Name)
});
}

NotifyPropertyChanged("fileslist");
}

如果您认为我一定是 WPF 新手,那么您是对的 :) 如果这是一个愚蠢的问题,我深表歉意。请帮忙。

XAML:

<ListView Name="lvfiles" Grid.Row="4" ItemsSource="{Binding fileslist}" SelectionMode="Single" SelectedItem="{Binding SelectedFiles}" DataContext="{Binding }" Style="{StaticResource ListView}">
<ListView.View>
<GridView x:Name="gridFiles">
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Tag="{Binding ID}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn x:Name="FileId" Header="#" DisplayMemberBinding="{Binding FileId}" Width="Auto"/>

<GridViewColumn x:Name="FileName" Header="{inf:Loc ConfigurationsName}" Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding FileName, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" BorderThickness="0" Style="{StaticResource ListViewTextBoxes}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>

<!--<GridViewColumn x:Name="FileName" Header="{inf:Loc ConfigurationsName}" DisplayMemberBinding="{Binding FileName}" Width="Auto"/>-->
<GridViewColumn x:Name="FileCreated" Header="{inf:Loc ConfigurationsCreated}" DisplayMemberBinding="{Binding FileCreated}" Width="Auto"/>
<GridViewColumn x:Name="FileChanged" Header="{inf:Loc ConfigurationsChanged}" DisplayMemberBinding="{Binding FileChanged}" Width="Auto"/>
</GridView>
</ListView.View>
</ListView>

最佳答案

在主菜单中,您应该为 PropertyChanged 事件分配一个处理程序,您可以在其中更新列表。

DataDiles.PropertyChanged+= DataFiles_PropertyChanged;

public void DataFiles_PropertyChanged(/*...*/)
{
//Refresh the list
}

如需更准确的代码,请编辑您的问题并完整添加 DataFiles 类的定义。
希望对您有所帮助。

关于c# - 从另一个类更新时更新 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32226048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com