在wpf中单击按钮添加内容

时间:2012-03-24 00:59:43

标签: c# wpf xaml

为什么我不能在wpf中这样做?

    button1Click.Content = "Hover over me";

    ToolTip t = new ToolTip();
    t.Content = "Something helpful";
    button1Click.Tooltip = t;

我在初始化时使用填充按钮填充我的寡妇然后有一个foreach循环,添加如下按钮:

     foreach (var routedEventHandler in new RoutedEventHandler[] { button1Click, button2Click, button3_Click })`

现在,在这个区域,我将样式一次性应用于按钮:

public void populateButtons()
        {
            double xPos;
            double yPos;


            Random ranNum = new Random();
            foreach (var routedEventHandler in new RoutedEventHandler[] { button1Click, button2Click, button3_Click })
            {

                Button foo = new Button();



                Style buttonStyle = Window.Resources["CurvedButton"] as Style;
                int sizeValue = 100;

                foo.Width = sizeValue;
                foo.Height = sizeValue;

                xPos = ranNum.Next(200);
                yPos = ranNum.Next(250);


                foo.HorizontalAlignment = HorizontalAlignment.Left;
                foo.VerticalAlignment = VerticalAlignment.Top;
                foo.Margin = new Thickness(xPos, yPos, 0, 0);
                foo.Style = buttonStyle;

                foo.Click += routedEventHandler;

                LayoutRoot.Children.Add(foo);
            }
        }
    }
}

但是当我尝试这个时:

    private void button3_Click(object sender, RoutedEventArgs e)
    {
        ((Button)sender).ToolTip = t;
    }

仅在按下按钮时激活? (谢谢@ H.B)

但是,当我将工具提示代码粘贴到我的填充按钮中时:

        Random ranNum = new Random();
        foreach (var routedEventHandler in new RoutedEventHandler[] { button1Click, button2Click, button3_Click })
        {

            Button foo = new Button();
            ToolTip t = new ToolTip();
            t.Content = "Something helpful";
            foo.ToolTip = t;

有效吗?但问题是,这样做会设置所有按钮具有相同的工具提示和/或按钮内容!我不想要,但我无法找到解决方法吗?

总而言之,我可以在“populateButtons()”中使用相同的工具提示消息或按钮内容设置所有按钮,或者我只能在按下按钮时设置工具提示和按钮内容。

是否有可能将内容添加到命名按钮的方法?

就像我最初的尝试一样:

button1Click.Content = "Home Button";

button1Click.ToolTip = "Hover over me";

为什么在初始化时无法为特定按钮设置内容和工具提示?

3 个答案:

答案 0 :(得分:1)

您没有在处理程序中分配工具提示,那么应该怎么做?

((Button)sender).ToolTip = t;

答案 1 :(得分:1)

如果你想走这条路,那么你可以为Loaded事件添加一个处理程序:

foo.Click += routedEventHandler;
foo.Loaded += routedEventHandler;

然后你有类似的东西:

void button2Click(object sender, RoutedEventArgs e)
    {
        if (e.RoutedEvent == FrameworkElement.LoadedEvent)
        {
            ToolTip t = new ToolTip();
            t.Content = "Something helpful";
            ((Button)sender).ToolTip = t;
            return;
        }
        //Logic for handling button clicks goes here
        MessageBox.Show("action 2");
    }

答案 2 :(得分:0)

如果我理解正确,您希望使用不同的“类型”按钮,每个按钮具有不同的Click处理程序和不同的工具提示。

如果是这种情况,您可以创建从Button派生的不同类(例如,HelpButton,UnhelpfulButton ...请选择好名字:))并在其构造函数中指定处理程序和工具提示。

然后,在主代码中,您可以循环遍历不同的类,并将它们的实例直接添加到主画布。

另一种可能的选择是使用命令创建不同的“模板”按钮并从中复制属性而不是子类化Button。像这样:

Button helpfulTemplate = new Button { ToolTip = "blah" };
helpfulTemplate.Command = HelpfulCommand;

Button unhelpfulTemplate = new Button { ToolTip = "bloh" };
unhelpfulTemplate.Command = UnhelpfulCommand;

foreach(var template in new Button[] {helpfulTemplate, unhelpfulTemplate})
{
     var newCopy = new Button();
     //etc.
     newCopy.ToolTip = template.ToolTip;
     newCopy.Command = template.Command;
}
相关问题