我已經使用 Itemtemplate 和 Datatemplate 在 Wpf 中創建了一個串列框:
<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 檔案中。
現在我試圖在我的串列框中保存更改,但是我如何才能訪問我在串列框中的專案?.
這是我的 Observable 串列和屬性類的代碼。
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.
}
我試過 For each 來獲得訪問權限,我試過 for 回圈,但我無法訪問。
和:
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);
我可以看到他可以訪問這些專案,但他列印了以下內容: 在此處輸入影像描述
如何列印正確的值?
uj5u.com熱心網友回復:
您不應訪問控制元件來獲取資料項。而是直接訪問源集合:
主視圖模型.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);
}
}
主視窗.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();
}
主視窗.xaml
<Window>
<ListBox itemsSource="{Binding TodoItems}">
...
</ListBox>
</Window>
C# 中的屬性必須如下所示(注意正確的大小寫:欄位使用駝峰命名,所有其他成員使用 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://www.uj5u.com/qiye/388627.html
