How to Create New Panels on Button Click?

user2835256

My WPF application Interface demands something really different. Actually there are dynamic buttons in my application. Now i want that as i click on Button it's Panel should create and open to add something in it's textbox to save in database! On the same way Format TextBox should update with opened Panels as this image below is showing you all.

How to differentiate between panels textboxes to save text in their textboxes in database?

enter image description here

EDIT:

I am not using MVVM!

Daniel Gruszczyk

I hope you are just asking for a general approach to the problem, as I can't be bothered to code all that stuff.
So here it goes:

  1. create a user-control for your Panels. Since they all the same, use a property to store value for which button the panel is created.
  2. create an ObservableCollection of your Panel controls, you will store your newly added panels here.
  3. create a ItemsControl to show your panels and assign your ObservableCollection as ItemsSource of that panel. This way if you add something to the collection, it will show on your GUI
  4. attach an event to your buttons, in this event you would (instantiate a new panel) -> (set ID of the panel to "button1", "button2", "button3" or however you want to distinguish) -> (add the panel to the ObservableCollection)
  5. When you finish and want to get all the panels etc, you can simply loop through your ObservableCollection and select pannels/values there. You can also use linq to filter panels by button ID etc.

Any more questions, just ask :)

--EDIT--
1. As I have said, create a user control that will be your panel. This way you can instantiate it in the code (xaml.cs file) on a button-click event, just as you would instantiate object of any other class.
2. When you are creating your panel user-control, let's call it MyPanel, add a property in the code behind and call it whatever you want (ParentButton maybe?). Now you could add a constructor that would take the button ID as parameter, or you could simply use standard constructor and then assign the button ID to that property before adding the panel to the collection.

--EDIT--
Here is a link to my struggles with ObservableCollection. There you can find a quick and simple way to attach collections to Lists and handling changes.
Microsoft tutorial on INotifyPropertyChanged, you would implement that interface in your panel (probably)
Microsoft tutorial on ObservableCollections.
Microsoft tutorial on how to bind data to lists etc.
Now remember: they use simple objects in their tutorials, but you can use user-control in the same way.
Again, it is a lot of reading, but you gotta do it yourself... Good luck :)

--EDIT--
Actually, you don't even have to create a user control, just create an object with data you want to hold, like that:

class MyPanel: INotifyPropertyChanged
{
    private string _id;
    private string _text;

    public string Id
    {
        get { return _id; }
        set
        {
            if (value != _id)
            {
                _id = value;
                NotifyPropertyChanged();
            }
        }
    }
    public string Text
    {
        get { return _text; }
        set
        {
            if (value != _text)
            {
                _text= value;
                NotifyPropertyChanged();
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Then add a control to your app window, where you want to show your panels:

<ItemsControl Name="lstPanels" ScrollViewer.VerticalScrollBarVisibility="Auto">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel orientation="vertical">
                        <TextBlock Text="{Binding Id}">
                        <TextBox Text="{Binding Text, Mode=TwoWay}" />
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

Then add an observable collection:

private ObservableCollection<MyPanel> panels = new ObservableCollection<MyPanel>();

And in constructor attach it to your ItemControl:

lstPanels.ItemsSource = panels;

Then add a function to add a new panel:

private void addPanel(string buttonId)
{
    MyPanel p = new MyPanel { Id = buttonId; };
    panels.add(p);
}

Then on your button1/button2/button3 click call that function with appropriate id:

addPanel("button1");
addPanel("button2");

When you want to finish and loop through panels, use standard foreach or for loop on panels :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to Create New Panels on Button Click?

From Dev

How to create a new window when I click a button in QT?

From Dev

Change panels with one button of a click

From Dev

Click an enter button to create a new textbox

From Dev

create new html elements for button click in query

From Dev

Android: Create new ImageView by every button click

From Dev

Trying to get panels to disappear with the click of a button

From Dev

Add panels inside a panel dynamically on button click

From Dev

Trying to get panels to disappear with the click of a button

From Dev

Android: How does one go about having the user click a button to create (not go to) a new activity

From Dev

How do I decide whether to create a new window when i Click on a button

From Java

How to start new activity on button click

From Dev

How to add a new ObservableCollection on Button click?

From Dev

How to create div with different class on button click?

From Dev

How create datatables dynamically on button click

From Dev

How will i create a loop inside a button click

From Dev

How to create div with different class on button click?

From Dev

How to dynamically create objects on the fly with click of a button

From Dev

How to create html code when click button?

From Dev

Swift: How to create UIActionSheet with click on button UITableVieCell

From Dev

Dynamically create and add new component on button click in Angular Dart

From Dev

Create a new thread in click button event c++

From Dev

create a new menu option menu on button click in the same activity

From Dev

Create a New Record in a SubForm on button click MS Access 2013 VBA

From Dev

Android Arraylist Baseadapter button click Create new Arraylist with no duplicacy

From Dev

How to create Button_Click() Event for Bootstrap Button?

From Dev

how to create button dynamically and add click event to button in android

From Dev

How to create a new button in WordPress admin menu

From Dev

Create an apk on button click

Related Related

  1. 1

    How to Create New Panels on Button Click?

  2. 2

    How to create a new window when I click a button in QT?

  3. 3

    Change panels with one button of a click

  4. 4

    Click an enter button to create a new textbox

  5. 5

    create new html elements for button click in query

  6. 6

    Android: Create new ImageView by every button click

  7. 7

    Trying to get panels to disappear with the click of a button

  8. 8

    Add panels inside a panel dynamically on button click

  9. 9

    Trying to get panels to disappear with the click of a button

  10. 10

    Android: How does one go about having the user click a button to create (not go to) a new activity

  11. 11

    How do I decide whether to create a new window when i Click on a button

  12. 12

    How to start new activity on button click

  13. 13

    How to add a new ObservableCollection on Button click?

  14. 14

    How to create div with different class on button click?

  15. 15

    How create datatables dynamically on button click

  16. 16

    How will i create a loop inside a button click

  17. 17

    How to create div with different class on button click?

  18. 18

    How to dynamically create objects on the fly with click of a button

  19. 19

    How to create html code when click button?

  20. 20

    Swift: How to create UIActionSheet with click on button UITableVieCell

  21. 21

    Dynamically create and add new component on button click in Angular Dart

  22. 22

    Create a new thread in click button event c++

  23. 23

    create a new menu option menu on button click in the same activity

  24. 24

    Create a New Record in a SubForm on button click MS Access 2013 VBA

  25. 25

    Android Arraylist Baseadapter button click Create new Arraylist with no duplicacy

  26. 26

    How to create Button_Click() Event for Bootstrap Button?

  27. 27

    how to create button dynamically and add click event to button in android

  28. 28

    How to create a new button in WordPress admin menu

  29. 29

    Create an apk on button click

HotTag

Archive