在 WPF 中添加动态按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19517292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding dynamic buttons in WPF
提问by Dim
I made code that creates buttons dynamically, but how to assign different function to each button?
我编写了动态创建按钮的代码,但是如何为每个按钮分配不同的功能?
for (int i = 0; i < Buttons.Count; i++)
{
Button newBtn = new Button();
newBtn.Content = Buttons[i];
newBtn.Name = "Button" + i.ToString();
newBtn.Height = 23;
stackPanel1.Children.Add(newBtn);
newBtn.Click += new RoutedEventHandler(newBtn_Click);
}
private void newBtn_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello");
}
Now each button shows "Hello", but I wish it to be "Hello1", "Hello2"....ect.
现在每个按钮都显示“Hello”,但我希望它是“Hello1”、“Hello2”……等等。
回答by blindmeis
if you could create a collection of objects with DelegateCommands or RelayCommand Property and DisplayName Property - you would just need a ItemsControl bind to this Collection and a DataTemplate for binding a button to Command and Text.
如果您可以使用 DelegateCommands 或 RelayCommand 属性和 DisplayName 属性创建对象集合 - 您只需要绑定到此集合的 ItemsControl 和用于将按钮绑定到命令和文本的 DataTemplate。
EDIT: just out of my head
编辑:就在我的脑海里
public class MyCommandWrapper
{
public ICommand Command {get;set;}
public string DisplayName {get;set;}
}
in your viewmodel
在你的视图模型中
public ObservableCollection<MyCommandWrapper> MyCommands {get;set;}
MyCommands.Add(new MyCommandWrapper(){Command = MyTestCommand1, DisplayName = "Test 1"};
...
in your xaml
在你的 xaml
<ItemsControl ItemsSource="{Binding MyCommands}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type local:MyCommandWrapper}">
<Button Content="{Binding DisplayName}" Command="{Binding Command}"/>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
EDIT 2: if you need a new dynamic button - just add a new wrapper to your collection
编辑 2:如果您需要一个新的动态按钮 - 只需在您的收藏中添加一个新的包装器
回答by Anatoly Nikolaev
for (int i = 0; i < Buttons.Count; i++)
{
Button newBtn = new Button();
newBtn.Content = Buttons[i];
newBtn.Height = 23;
newBtn.Tag=i;
stackPanel1.Children.Add(newBtn);
newBtn.Click += new RoutedEventHandler(newBtn_Click);
}
private void newBtn_Click(object sender, RoutedEventArgs e)
{
Button btn=sender as Button;
int i=(int)btn.Tag;
switch(i)
{
case 0: /*do something*/ break;
case 1: /*do something else*/ break;
default: /*do something by default*/ break;
}
}
回答by gleng
private void newBtn_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
var buttonNumber = button.Name.Remove(0, 6);
MessageBox.Show("Hello" + buttonNumber);
}
回答by Torben Schramme
Check the senderparameter of your newBtn_Clickfunction. It should contain the instance of the button that was clicked. You can cast it to a button and check the name:
检查函数的sender参数newBtn_Click。它应该包含被点击的按钮的实例。您可以将其投射到按钮并检查名称:
private void newBtn_Click(object sender, RoutedEventArgs e)
{
var btn = sender as Button;
if(btn != null)
{
MessageBox.Show(btn.Name);
}
}
If you don't want to check the Name property, you can also use the Tag property (http://msdn.microsoft.com/library/system.windows.frameworkelement.tag.aspx) to which you can assign an arbitrary object and check this later:
如果您不想检查 Name 属性,您还可以使用 Tag 属性(http://msdn.microsoft.com/library/system.windows.frameworkelement.tag.aspx),您可以为其分配任意对象并稍后检查:
Button newBtn = new Button();
newBtn.Tag = i;
And later in the click handler:
稍后在点击处理程序中:
private void newBtn_Click(object sender, RoutedEventArgs e)
{
var btn = sender as Button;
if(btn != null)
{
if(btn.Tag is int)
MessageBox.Show(String.Format("Hello{0}", btn.Tag));
}
}
I would prefer the solution with Tag because it's more safe than extracting something from a string.
我更喜欢使用 Tag 的解决方案,因为它比从字符串中提取内容更安全。
回答by tmp001
newBtn.Click += new RoutedEventHandler((s,e) => MessageBox.Show("hallo"+((Button)s).Name);

