0

I'm pretty new to programming WPF applications in C# and wondering the what the best method is to do the following:

I have a list of an unknown number of names displayed in a list box and want to have a way that when one is clicked it displays a form that can be filled in then saved, then when the next name is clicked it displays a new blank form to fill out, but if I return to any previous form it will display the information last entered.

I'm only looking for a basic answer as I know its quite a long winded question.

Thank you for all the help in advanced!

3
  • 1
    It's slightly unclear what aspect you're finding difficult. You'd create two windows - one for your "main" window and one for the form. I'd suggest using data binding with a view-model to display the data in the form - then when you open up the form (whether for the first time or not) you just populate the view model accordingly and set it as the data context. Commented Jun 30, 2022 at 17:52
  • 1
    That sounds like a class with two properties. NumberName and NumberValue. Define a viewmodel with them. Bind a collection of these to itemssource of a listview. One in each of 2 columns. Select one to choose it. In a panel to the side bind selecteditem of the listview as datacontext. Hind n that stackpanel or grid you need a textblock to label each and a couple more controls for the values. Bjnd the name to a textblock.text and value to a textbox.text. Commented Jun 30, 2022 at 18:06
  • "what the best method is to do the following" - implement all requirements exactly as written, step by step: 1. display a list of an unknown number names in a list box 2. have a way that when one is clicked it displays a form, etc. it is very simple. might take some time to code properly and make design straight but definitely not a rocket science Commented Jun 30, 2022 at 18:26

1 Answer 1

2

I know this is an unclear question, but I am working on a similar project right now, that's why I gonna try to answer. You say "want to have a way that when one is clicked it displays a form", I guess you should create class(let call it MyClass) that has some properties which you will use and is displayed in ListBox like Button, you can do it by DataTemplate and bind your ListBox to ObservableCollection<T> of your MyClass objects so your ListBox would automatically update when you add or remove something from there, don't forget to use ViewModel and Commands for you Buttons. For form part of your question, I think you can create class MyForm that is inherited from window class, that would have properties required for your project. For example you want to save text in your form that will be displayed when you click button second time.

public MyForm : Window
{
    private string FormsText { get; set;}
}

Then you can create List of MyForm class objects and save them in file with xml serialization that will save every property of your object and you can write logic based on this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.