Home > Software engineering >  Get Items from a Listbox in WPF and write in other List
Get Items from a Listbox in WPF and write in other List

Time:12-22

Ive created a Listbox in Wpf with a Itemtemplate and Datatemplate:

<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>

I can add/change/Remove Items in my listbox.

Now ive tried to save the Items in a txt file. If i Add a item, its work well, i can save them in my txt File.

Now ive tried to save changes in my Listbox, but how can i get access to my Items in a Listbox? .

Here is the Code behind for my Observable List and Property Class.

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));
        }
    }

My thought was, if im gonna to close my Window, Override the old List with the new Changed Items in my Listbox.

For this ive Created a Window close Event:

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.
       
    }

Ive tried with For each to get access, ive tried for loops but i cant get access.

With:

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);

I can see That he got access to the items but he print the following: enter image description here

How can i print the right values ?

CodePudding user response:

You should not access the control to get the data items. Rather access the source collection directly:

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>

Properties in C# must look like this (pay attention to the proper casing: use camelCase for fields and PascalCase for all other members). Also use nameof to specify the property's name:

private int fortschritt;
public int FortSchritt
{
  get => this.fortschritt; 
  set
  {
    if (value != this.fortschritt)  
    {
      this.fortschritt = value;
      this.NotifyPropertyChanged(nameof(this.Fortschritt));
    }
  }
}
  • Related