O to chodzi? Starałem się napisać kod żeby był jak najprostszy.
<Window x:Class="WpfPractice.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfPractice"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Grid>
<StackPanel x:Name="Panel">
<Button x:Name="Add" Content="Dodaj" Click="Add_Click"/>
</StackPanel>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfPractice
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private List<string> buttons = new List<string>();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
buttons = GetSetting("buttons").Split(';').ToList();
foreach (var item in buttons)
{
Panel.Children.Insert(0, new Button() { Content = item});
}
}
private void Add_Click(object sender, RoutedEventArgs e)
{
string newValue = new Random().Next(0, 100).ToString();
buttons.Add(newValue);
Panel.Children.Insert(0, new Button() { Content = newValue });
SetSetting("buttons", string.Join(";", buttons));
}
public string GetSetting(string key)
{
return ConfigurationManager.AppSettings[key]!;
}
public void SetSetting(string key, string value)
{
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configuration.AppSettings.Settings[key].Value = value;
configuration.Save(ConfigurationSaveMode.Full, true);
ConfigurationManager.RefreshSection("appSettings");
}
}
}