1

What my application(test app) do:
It is WPF application where UI is dynamically created.
First it loads with a window, main window with only one button, Add.
Then user can add a component, a Usercontrol. each time the add button is created a new component is added in the widow, each component has a remove button.

What i want to do:
After user add and remove any number of usercontrol components he can close the program, then after the program is loaded again (relaunched), previously added components must be loaded and to be able to continue add/remove.

what i have tried:
Saving the State of a WPF Application Page . This is like exactly what i want but the problem is it saves the state of the application and loads too, but then you can't do anything in the loaded application, neither the add nor remove is working. It just load the application and that's it.

How can i fix this? is there a way to make the buttons clickable or is this approach is read only? if so how to achieve this?

I checked on saving the settings approach but it is not possible as the UI elements are dynamically added. (right?)

component that is added

namespace WpfTest
{
/// <summary>
/// Interaction logic for UCTest.xaml
/// </summary>
public partial class UCTest : UserControl
{
    public UCTest()
    {
        InitializeComponent();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        removethis((StackPanel)this.Parent);
    }

    void removethis(StackPanel g) {

        g.Children.Remove(this);
    }

    public string setText
    {
        get
        {
            return this.textBlock.Text;
        }

        set
        {
            this.textBlock.Text = value;
        }

    }


}
}

main window

namespace WpfTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    ObservableCollection<UCTest> oc;

    public MainWindow()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainWindow_Loaded);
        Closing += new CancelEventHandler(MainWindow_Closing);

    }
    int cnt;
    private void button_Click(object sender, RoutedEventArgs e)
    {

        sub1.Children.Add(new UCTest() { setText=Convert.ToString(cnt++)});
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        LoadExternalXaml();
    }

    void MainWindow_Closing(object sender, CancelEventArgs e)
    {
        SaveExternalXaml();
    }

    public void LoadExternalXaml()
    {
        if (File.Exists(@"D:\Test.xaml"))
        {
            using (FileStream stream = new FileStream(@"D:\Test.xaml", FileMode.Open))
            {
                this.Content = XamlReader.Load(stream);
            }
        }
    }

    public void SaveExternalXaml()
    {
        using (FileStream stream = new FileStream(@"D:\Test.xaml", FileMode.Create))
        {
            XamlWriter.Save(this.Content, stream);
        }
    }
}
}
Community
  • 1
  • 1
Raveen Athapaththu
  • 190
  • 1
  • 1
  • 13
  • And there is another problem that i cannot add more than one component , when added an exception 'System.Windows.Markup.XamlParseException' is thrown, as it states it is because all the component added have the same name (object name, button name , textfield name) How to solve this? – Raveen Athapaththu May 25 '16 at 02:35
  • I have come across a similar requirement, did you find a solution for this ? – Clint Aug 13 '18 at 21:47

0 Answers0