我在Wpf中使用Itemtemplate和数据板创建了一个Listbox:
<ListBox Name="LBox" HorizontalContentAlignment="Stretch" Grid.Column="2" SelectionMode="Single">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="LTxtBox" Text="{Binding NAME}" Grid.Column="0"/>
<ProgressBar x:Name="PBarLbox" Grid.Column="1" Minimum="0" Maximum="100" Value="{Binding FORTSCHRITT}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我可以在我的列表框中添加/更改/删除项目。
现在,我尝试将这些项保存在txt文件中。如果我添加一个项目,它的工作良好,我可以将它们保存在我的txt文件中。
现在,我试图在Listbox中保存更改,但是如何访问Listbox中的项目?
下面是我的可观察列表和属性类的代码。
private ObservableCollection<TodoItem> Todo =new ObservableCollection<TodoItem>();
public class TodoItem : INotifyPropertyChanged
{
public string NAME { get; set; }
public int FORTSCHRITT{ get; set; }
//###########################################
public string Name
{
get { return this.NAME; }
set
{
if (this.NAME != value)
{
this.NAME = value;
this.NotifyPropertyChanged("NAME");
}
}
}
public int fortschritt
{
get { return this.FORTSCHRITT; }
set
{
if (this.FORTSCHRITT != value)
{
this.FORTSCHRITT = value;
this.NotifyPropertyChanged("FORTSCHRITT");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
我的想法是,如果我要关闭我的窗口,用列表框中新更改的项覆盖旧列表。
为此,我创建了一个窗口关闭事件:
void DataWindow_Closing(object sender, CancelEventArgs e)
{ // TODO
// Wenn er das Fenster schließt soll er alle Daten die im ToDo Fenster sind speichern und die alte Datei Überschreiben.
}
我试过让每个人都能访问,我尝试过循环,但我无法访问。
通过以下方式:
List<string> list = new List<string>();
string[] arr;
foreach (var x in LBox.Items)
{
list.Add(x.ToString());
}
arr= list.ToArray();
string display=String.Join(Environment.NewLine, arr);
MessageBox.Show(display);
我可以看到他可以访问这些项目,但他打印了以下内容:在这里输入图像描述
如何打印正确的值?
发布于 2021-12-21 11:54:50
不应访问控件以获取数据项。而是直接访问源集合:
MainViewModel.cs
class MainViewModel : INotifyPropertyChanged
{
public ObservableCollection<TodoItem> TodoItems { get; }
public MainViewModel()
{
this TodoItems = new ObservableCollection<TodoItem>();
}
public async Task SaveDataAsync()
{
var fileContentBuilder = new StringBuilder();
foreach (TodoItem todoItem in TodoItems)
{
fileContentBuilder.AppendLine($"{todoItem.Name}, {todoItem.Fortschritt}");
}
await using var destinationFileStream = File.Open("Destination_File_Path", FileMode.Create);
await using var streamWriter = new StreamWriter(destinationFileStream);
string fileContent = fileContentBuilder.ToString();
await streamWriter.WriteAsync(fileContent);
}
}
MainWindow.xaml.cs
public partial class MainWindow : Window
{
private MainViewModel MainVieModel { get; }
public MainWindow()
{
InitilaizeComponent();
this.MainViewModel = new MainViewModel();
this.DataContext = this.MainViewModel;
this.Closing += SaveDataToFile_OnClosing;
}
private async void SaveDataToFile_OnClosing(object sender, CancelEventArgs e)
=> await this.MainViewModel.SaveDataAsync();
}
MainWindow.xaml
<Window>
<ListBox itemsSource="{Binding TodoItems}">
...
</ListBox>
</Window>
C#中的属性必须如下所示(注意正确的大小写:字段使用camelCase,其他成员使用PascalCase )。还可以使用nameof
指定属性的名称:
private int fortschritt;
public int FortSchritt
{
get => this.fortschritt;
set
{
if (value != this.fortschritt)
{
this.fortschritt = value;
this.NotifyPropertyChanged(nameof(this.Fortschritt));
}
}
}
https://stackoverflow.com/questions/70434220
复制相似问题