Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7678109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:34:10+00:00 2026-05-31T17:34:10+00:00

If buttons are dynamically added in wpf from code behind how can you add

  • 0

If buttons are dynamically added in wpf from code behind how can you add tooltips to them or text to the actual button?

This is what you would do in windows forms but I dont think you can do it in wpf:

partial class Window1
{
    void button3Click(object sender, RoutedEventArgs e)
    {
        //toolTip1.SetToolTip(this.button1, "My button1");
        MessageBox.Show("action 3");
    }
    void button2Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("action 2");
    }
    void button1Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("action 1");
    }

    public Window1()
    {
        this.InitializeComponent();
        populateButtons();
        //ToolTip toolTip1 = new ToolTip();

        //// Set up the delays for the ToolTip.
        //toolTip1.AutoPopDelay = 5000;
        //toolTip1.InitialDelay = 1000;
        //toolTip1.ReshowDelay = 500;
        //// Force the ToolTip text to be displayed whether or not the form is active.
        //toolTip1.ShowAlways = true;

    }

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


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

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

            //canvas1.HorizontalAlignment = HorizontalAlignment.Left;
            //canvas1.VerticalAlignment = VerticalAlignment.Top;
            //canvas1.Margin = new Thickness(xPos, yPos, 0, 0);

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

}

As for adding text im not sure?

Here is the xaml:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xml:lang="en-US"
    x:Class="DynamicButtons.Window1"
    x:Name="Window"
    Title="Dynamic Buttons"
    Width="840" Height="600" Icon="shape_group.png">
    <Window.Resources>   
            <Style x:Key="CurvedButton" BasedOn="{x:Null}" TargetType="{x:Type Button}">

                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <ControlTemplate.Resources>
                                <Storyboard x:Key="OnMouseMove1">
                                    <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                        <SplineColorKeyFrame KeyTime="00:00:00" Value="#FFFFFFFF"/>
                                        <SplineColorKeyFrame KeyTime="00:00:00.3000000" Value="#7CE1DBDB"/>
                                    </ColorAnimationUsingKeyFrames>
                                    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
                                        <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
                                        <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1.66"/>
                                    </DoubleAnimationUsingKeyFrames>
                                    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
                                        <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>
                                        <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1.66"/>
                                    </DoubleAnimationUsingKeyFrames>
                                </Storyboard>
                                <Storyboard x:Key="OnMouseLeave1">
                                    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
                                        <SplineDoubleKeyFrame KeyTime="00:00:00.8000000" Value="1.78"/>
                                        <SplineDoubleKeyFrame KeyTime="00:00:01" Value="1"/>
                                    </DoubleAnimationUsingKeyFrames>
                                    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
                                        <SplineDoubleKeyFrame KeyTime="00:00:00.8000000" Value="1.78"/>
                                        <SplineDoubleKeyFrame KeyTime="00:00:01" Value="1"/>
                                    </DoubleAnimationUsingKeyFrames>
                                </Storyboard>
                                <Storyboard x:Key="OnClick1">
                                    <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                        <SplineColorKeyFrame KeyTime="00:00:00.2000000" Value="#FFFFFFFF"/>
                                        <SplineColorKeyFrame KeyTime="00:00:00.3000000" Value="#BFA0D1E2"/>
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </ControlTemplate.Resources>
                            <Grid>
                                <Rectangle RenderTransformOrigin="1,1" Fill="#3FFFFFFF" Stroke="{x:Null}" RadiusX="11" RadiusY="11" x:Name="rectangle">
                                    <Rectangle.RenderTransform>
                                        <TransformGroup>
                                            <ScaleTransform ScaleX="1" ScaleY="1"/>
                                            <SkewTransform AngleX="0" AngleY="0"/>
                                            <RotateTransform Angle="0"/>
                                            <TranslateTransform X="0" Y="0"/>
                                        </TransformGroup>
                                    </Rectangle.RenderTransform>
                                </Rectangle>
                                <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <EventTrigger RoutedEvent="ButtonBase.Click">
                                    <BeginStoryboard x:Name="OnClick1_BeginStoryboard" Storyboard="{StaticResource OnClick1}"/>
                                </EventTrigger>
                                <EventTrigger RoutedEvent="Mouse.MouseLeave">
                                    <BeginStoryboard x:Name="OnMouseLeave1_BeginStoryboard" Storyboard="{StaticResource OnMouseLeave1}"/>
                                </EventTrigger>
                                <EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
                                <EventTrigger RoutedEvent="Mouse.MouseEnter">
                                    <BeginStoryboard x:Name="OnMouseMove1_BeginStoryboard" Storyboard="{StaticResource OnMouseMove1}"/>
                                </EventTrigger>
                                <Trigger Property="IsFocused" Value="True"/>
                                <Trigger Property="IsDefaulted" Value="True"/>
                                <Trigger Property="IsMouseOver" Value="True"/>
                                <Trigger Property="IsPressed" Value="True"/>
                                <Trigger Property="IsEnabled" Value="False"/>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>

                <Setter Property="Background">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                            <GradientStop Color="#FFF3F3F3" Offset="0"/>
                            <GradientStop Color="#FFEBEBEB" Offset="0.5"/>
                            <GradientStop Color="#FFDDDDDD" Offset="0.5"/>
                            <GradientStop Color="#E1CDCDCD" Offset="1"/>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </Style>
        </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
    </Window.Triggers>
    <Window.Background>
        <LinearGradientBrush EndPoint="0.484,0.543" StartPoint="0.478,0.009">
            <GradientStop Color="Gray" Offset="1"/>
            <GradientStop Color="DarkGray" Offset="0"/>
        </LinearGradientBrush>
    </Window.Background>

        <Grid x:Name="LayoutRoot">

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Canvas Height="282" HorizontalAlignment="Left" Margin="12,12,0,0" Name="canvas1" VerticalAlignment="Top" Width="343" />
    </Grid>

</Window>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:34:12+00:00Added an answer on May 31, 2026 at 5:34 pm

    Have you tried to use the Tooltip-Property?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so this code here dynamically adds buttons to my wpf windows application. I cant
I have a page with some dynamically added buttons. If you click a button
In WPF C#, in code behind, I have to dynamically create an Array of
Does anyone have a quick and easy method for removing dynamically added buttons from
I am dynamically added a bunch of buttons to a toolbar. I want the
I'm generating buttons dynamically that I want it to call a method like this:
I have associated a Button ID while generating Buttons dynamically and created a Button
I'm using jQMobile 1.0, and building a list of radio buttons dynamically from json
I'm trying to dynamically create buttons at runtime with PyQT4.7 However, this being my
Looking for an example of how to dynamically add controls from the activity. Inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.