Kirsty White
Kirsty White

Reputation: 1220

adding content to a button click in wpf

Why cant I do this in wpf?

    button1Click.Content = "Hover over me";

or

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

I populate my widow with populate buttons on initialization I then have a foreach loop that adds buttons like so:

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

Now in this area I apply styles to the buttons all in one go like so:

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);
            }
        }
    }
}

But when I try this:

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

It only activates when a button is pressed? (Thanks @H.B)

However when I paste the tooltip code in my populate buttons:

        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;

It works? But the problem is that doing it this way sets all buttons with the same tooltip and or button content! which I dont want, but I cant find a way around it?

So to summarize I can either set all buttons with the same tooltip message or button content within "populateButtons()" or I can only set tooltips and button content when the button has been pressed.

Is there no method possible that can add content to a named button?

Like my initial attempt:

button1Click.Content = "Home Button";

or

button1Click.ToolTip = "Hover over me";

Why on earth cant you set content and tooltips for specific buttons on initialization?

Upvotes: 1

Views: 915

Answers (3)

Raj Ranjhan
Raj Ranjhan

Reputation: 3917

If you want to go this route then you can add a handler for Loaded event:

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

And then you have something like:

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");
    }

Upvotes: 1

robertos
robertos

Reputation: 1796

If I understand correctly, you want to have different "types" of buttons, each one with a different Click handler and a different Tooltip.

If that's the case, You could create different classes deriving from Button (e.g. HelpfulButton, UnhelpfulButton... choose good names please :) ) and in their constructor assign the handler and tooltip.

Then, in the main code, you could loop through the different classes and add instances of them directly to the main canvas.

Another possible option is to create different "template" buttons using commands and copy the properties from them instead of subclassing Button. Something like this:

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;
}

Upvotes: 0

brunnerh
brunnerh

Reputation: 184296

You are not assigning the tooltip in the handler, so how should anything happen?

((Button)sender).ToolTip = t;

Upvotes: 1

Related Questions