【问题标题】:adding content to a button click in wpf将内容添加到wpf中的按钮单击
【发布时间】:2012-03-24 11:10:23
【问题描述】:

为什么我不能在 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";

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

【问题讨论】:

标签: c# wpf xaml


【解决方案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】:

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

((Button)sender).ToolTip = t;

【讨论】:

  • 我已经更新了我的问题,这可能会提供更清晰的信息。读完后,我意识到它只对我有意义。
【解决方案3】:

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

如果是这种情况,您可以创建从 Button 派生的不同类(例如 HelpfulButton、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;
}

【讨论】:

  • 我在 foreach 中使用它的方式是在主窗体上随机创建并放置 3 个按钮,我要做的就是为它们创建自定义工具提示和按钮名称。这个方法阻止我调用这三个按钮,因为它们现在没有命名,并且命令仍然只概括按钮它不是特定的所以按钮 1 不能在它上面打招呼,而所有其余的按钮都打招呼。我也不能将方法组转换为 ICommand?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
  • 2021-11-01
  • 1970-01-01
  • 2011-03-03
相关资源
最近更新 更多