5

I'm about to develop my first WPF.

I want to get a list of buttons. The buttons get generated by ONE "Add"-button at the top of the WPF.

So when i press "Add" a new buttons comes up in the list.

First how i create the list? With a ListBox or a StackPanel? I think for the look a StackPanel would be nice but im not sure about how to add buttons there ...

And the other question: Normally, when i generate an object (i come from Java) each object gets a unique instance. But how i give every button a unique name?

I hope you can help me

3
  • 1
    WPF can be a bit tricky if you want to do it right. I suggest you get a good book on the matter, StackOverflow is not the right format for such a broad question. Commented Dec 18, 2014 at 21:28
  • Please narrow down your question and show your code clearly stating its deficiencies, so we can help. Best regards, Commented Dec 18, 2014 at 21:29
  • Thank you - the book is already ordered and i hope it will be in my Mailbox on Friday Commented Dec 18, 2014 at 21:36

2 Answers 2

26

Stop. Right now.

Before you do anything, learn basic MVVM. WPF is not WinForms (or its Java equivalent). You shouldn't be programmatically altering the UI until you know when you should do that.

Those buttons should represent data. That data should be in your view model somewhere. Then you would have an ItemsControl like this:

<ItemsControl ItemsSource="{Binding MyCollection}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button ... />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Now you can get all the unique info you need based on the bound object (no need for unique names). You can even make ItemsControl use a StackPanel as the underlying panel (incidentally, it does by default).

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

5 Comments

I would agree with @BradleyDotNET - you should adhere to a different software paradigm pertinent to WPF app dev. Best regards,
Would the downvoter care to comment?
Im currently not understanding you 100%. Is your example like a template for buttons that get created?
@jens12345675432 Yes, thats exactly what it does. As I noted in comments on the other answer, I would make sure to get a handle on MVVM and basic bindings before attempting data templates. Sounds like you get the idea though.
Thank you for your help - i have ordered a book yesterday but wasnt able to wait. I will continue reading about DataTemplates in the web for now
1

Since you need dynamic behavior and this is your first WPF app, you should write this on the code behind.

Just name your StackPanel something (add the name attribute to it), and handle the click event in the button (just double click the button in the WPF visual editor).

Inside the handler for the click event, you can do something like this:

this.MyStackPanel.Children.Add(new Button());

Of course, to add behavior to that button, you can assign it to a variable and add the proper event handlers.

5 Comments

Since this is his first WPF app, please just learn the right way to do this. Granted, this will work, its just not a good idea.
Not sure I agree, ItemsControl is (imo) a bit of an advanced topic in wpf and I'm not sure a beginner would be able to grasp it completely.
@BradleyDotNET, it's a matter of opinion, of course. But in my view, WPF has a steep learning curve, and mingling it with MVVM can lead to a few weeks before the OP can do anything. It's good to learn in small steps.
@TejasSharma and Bruno. Unquestionably jumping straight to templates is not a good starting point. Thats why I said he should learn the MVVM paradigm before doing this. This is a good second or third project to do. He needs to understand basic binding first. But getting bad habits (like programmatically manipulating the UI) isn't doing him any favors.
I agree writing on the code behind is not an issue, if it's your first app. I wouldn't worry too much because the XAML is just defining objects anyway.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.