티스토리 뷰

WPF

Dynamic Button 생성, Event

NicSub 2019. 9. 3. 09:22
728x90
반응형




Xaml code:​​​​​​​

<Window x:Class = "Test.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   Title="MainWindow" Height="350" Width="525">

<UniformGrid x:Name="grid">

</UniformGrid>

</Window>

public MainWindow()
{
InitializeComponent();

for (int i = 0; i < 10; ++i)
{
Button button = new Button()
{
Content = string.Format("Button for {0}", i),
Tag = i
};
button
.Click += new RoutedEventHandler(button_Click);
this.grid.Children.Add(button);
}
}

void button_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine(string.Format("You clicked on the {0}. button.", (sender as Button).Tag));
}

링크:

https://stackoverflow.com/questions/5929710/dynamically-add-multiple-buttons-to-wpf-window

 

Dynamically add multiple buttons to wpf window?

how would i add multiple buttons to a window in c#? here's what i need to do... i'm getting multiple user values from a dictionary (within reason, only @ 5-6 values). for each value, i need to cre...

stackoverflow.com

 

반응형
댓글