WPF C# 为动态创建的按钮创建 Click 事件
WPF C# create Click event for dynamically created button
我正在做一个项目,其中创建了 x 次新按钮(为了这个问题:x 目前由用户输入定义。在最终版本中,这将由SQL-数据库中的条目)。
然后这些按钮进入 ScrollViewer。
我需要每个动态创建的按钮都有一个独特的点击事件。
因此,如果单击按钮 1,则会执行事件 1,依此类推...
这当然意味着如果创建了 5 个按钮,则每个按钮都需要触发自己的功能。
函数的名称可以命名为 BtnClick + i。
那么什么是可行的解决方案呢?
无法让 EventHandler 按我需要的方式工作。
代码如下:
private void TextEnter(object sender, KeyEventArgs e)
{
if (Keyboard.IsKeyDown(Key.Enter))
{
TextBox testBox = (TextBox)sender;
int entry = Int32.Parse(testBox.Text);
Console.WriteLine(entry);
for (int i = 1; i < entry + 1; i++)
{
Button testBtn = new Button();
testBtn.Content = i;
testBtn.FontSize = 20;
testBtn.Foreground = new SolidColorBrush(Colors.White);
testBtn.Width = ListPanel.ActualWidth;
testBtn.Height = 60;
testBtn.FontWeight = FontWeights.Bold;
testBtn.Background = new SolidColorBrush(Colors.Transparent);
testBtn.Click += new EventHandler();
ListPanel.Children.Add(testBtn);
}
您可以使用相同的事件处理程序并打开 Button
的 Content
:
private void TextEnter(object sender, KeyEventArgs e)
{
if (Keyboard.IsKeyDown(Key.Enter))
{
...
for (int i = 1; i < entry + 1; i++)
{
Button testBtn = new Button();
testBtn.Content = i;
testBtn.FontSize = 20;
testBtn.Foreground = new SolidColorBrush(Colors.White);
testBtn.Width = ListPanel.ActualWidth;
testBtn.Height = 60;
testBtn.FontWeight = FontWeights.Bold;
testBtn.Background = new SolidColorBrush(Colors.Transparent);
testBtn.Click += TestBtn_Click;
ListPanel.Children.Add(testBtn);
}
}
}
private void TestBtn_Click(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
int content = Convert.ToInt32(button.Content);
switch (content)
{
case 1:
//do something for the first button
break;
case 2:
//do something for the second button...
break;
}
}
或者使用匿名内联函数创建事件处理程序:
testBtn.Click += (ss,ee) => { ShowPage(i); };
我正在做一个项目,其中创建了 x 次新按钮(为了这个问题:x 目前由用户输入定义。在最终版本中,这将由SQL-数据库中的条目)。 然后这些按钮进入 ScrollViewer。 我需要每个动态创建的按钮都有一个独特的点击事件。 因此,如果单击按钮 1,则会执行事件 1,依此类推... 这当然意味着如果创建了 5 个按钮,则每个按钮都需要触发自己的功能。 函数的名称可以命名为 BtnClick + i。 那么什么是可行的解决方案呢? 无法让 EventHandler 按我需要的方式工作。
代码如下:
private void TextEnter(object sender, KeyEventArgs e)
{
if (Keyboard.IsKeyDown(Key.Enter))
{
TextBox testBox = (TextBox)sender;
int entry = Int32.Parse(testBox.Text);
Console.WriteLine(entry);
for (int i = 1; i < entry + 1; i++)
{
Button testBtn = new Button();
testBtn.Content = i;
testBtn.FontSize = 20;
testBtn.Foreground = new SolidColorBrush(Colors.White);
testBtn.Width = ListPanel.ActualWidth;
testBtn.Height = 60;
testBtn.FontWeight = FontWeights.Bold;
testBtn.Background = new SolidColorBrush(Colors.Transparent);
testBtn.Click += new EventHandler();
ListPanel.Children.Add(testBtn);
}
您可以使用相同的事件处理程序并打开 Button
的 Content
:
private void TextEnter(object sender, KeyEventArgs e)
{
if (Keyboard.IsKeyDown(Key.Enter))
{
...
for (int i = 1; i < entry + 1; i++)
{
Button testBtn = new Button();
testBtn.Content = i;
testBtn.FontSize = 20;
testBtn.Foreground = new SolidColorBrush(Colors.White);
testBtn.Width = ListPanel.ActualWidth;
testBtn.Height = 60;
testBtn.FontWeight = FontWeights.Bold;
testBtn.Background = new SolidColorBrush(Colors.Transparent);
testBtn.Click += TestBtn_Click;
ListPanel.Children.Add(testBtn);
}
}
}
private void TestBtn_Click(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
int content = Convert.ToInt32(button.Content);
switch (content)
{
case 1:
//do something for the first button
break;
case 2:
//do something for the second button...
break;
}
}
或者使用匿名内联函数创建事件处理程序:
testBtn.Click += (ss,ee) => { ShowPage(i); };