title: dynamically added buttons (tooltips and text)
body: <p>If buttons are dynamically added in wpf from code behind how can you add tooltips to them or text to the actual button?</p>
  
  <p>This is what you would do in windows forms but I dont think you can do it in wpf:</p>
  
  <pre><code>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);
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>As for adding text im not sure?</p>
  
  <p>Here is the xaml:</p>
  
  <pre><code>&lt;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"&gt;
      &lt;Window.Resources&gt;   
              &lt;Style x:Key="CurvedButton" BasedOn="{x:Null}" TargetType="{x:Type Button}"&gt;
  
                  &lt;Setter Property="Template"&gt;
                      &lt;Setter.Value&gt;
                          &lt;ControlTemplate TargetType="{x:Type Button}"&gt;
                              &lt;ControlTemplate.Resources&gt;
                                  &lt;Storyboard x:Key="OnMouseMove1"&gt;
                                      &lt;ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"&gt;
                                          &lt;SplineColorKeyFrame KeyTime="00:00:00" Value="#FFFFFFFF"/&gt;
                                          &lt;SplineColorKeyFrame KeyTime="00:00:00.3000000" Value="#7CE1DBDB"/&gt;
                                      &lt;/ColorAnimationUsingKeyFrames&gt;
                                      &lt;DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"&gt;
                                          &lt;SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/&gt;
                                          &lt;SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1.66"/&gt;
                                      &lt;/DoubleAnimationUsingKeyFrames&gt;
                                      &lt;DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"&gt;
                                          &lt;SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/&gt;
                                          &lt;SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1.66"/&gt;
                                      &lt;/DoubleAnimationUsingKeyFrames&gt;
                                  &lt;/Storyboard&gt;
                                  &lt;Storyboard x:Key="OnMouseLeave1"&gt;
                                      &lt;DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"&gt;
                                          &lt;SplineDoubleKeyFrame KeyTime="00:00:00.8000000" Value="1.78"/&gt;
                                          &lt;SplineDoubleKeyFrame KeyTime="00:00:01" Value="1"/&gt;
                                      &lt;/DoubleAnimationUsingKeyFrames&gt;
                                      &lt;DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"&gt;
                                          &lt;SplineDoubleKeyFrame KeyTime="00:00:00.8000000" Value="1.78"/&gt;
                                          &lt;SplineDoubleKeyFrame KeyTime="00:00:01" Value="1"/&gt;
                                      &lt;/DoubleAnimationUsingKeyFrames&gt;
                                  &lt;/Storyboard&gt;
                                  &lt;Storyboard x:Key="OnClick1"&gt;
                                      &lt;ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"&gt;
                                          &lt;SplineColorKeyFrame KeyTime="00:00:00.2000000" Value="#FFFFFFFF"/&gt;
                                          &lt;SplineColorKeyFrame KeyTime="00:00:00.3000000" Value="#BFA0D1E2"/&gt;
                                      &lt;/ColorAnimationUsingKeyFrames&gt;
                                  &lt;/Storyboard&gt;
                              &lt;/ControlTemplate.Resources&gt;
                              &lt;Grid&gt;
                                  &lt;Rectangle RenderTransformOrigin="1,1" Fill="#3FFFFFFF" Stroke="{x:Null}" RadiusX="11" RadiusY="11" x:Name="rectangle"&gt;
                                      &lt;Rectangle.RenderTransform&gt;
                                          &lt;TransformGroup&gt;
                                              &lt;ScaleTransform ScaleX="1" ScaleY="1"/&gt;
                                              &lt;SkewTransform AngleX="0" AngleY="0"/&gt;
                                              &lt;RotateTransform Angle="0"/&gt;
                                              &lt;TranslateTransform X="0" Y="0"/&gt;
                                          &lt;/TransformGroup&gt;
                                      &lt;/Rectangle.RenderTransform&gt;
                                  &lt;/Rectangle&gt;
                                  &lt;ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/&gt;
                              &lt;/Grid&gt;
                              &lt;ControlTemplate.Triggers&gt;
                                  &lt;EventTrigger RoutedEvent="ButtonBase.Click"&gt;
                                      &lt;BeginStoryboard x:Name="OnClick1_BeginStoryboard" Storyboard="{StaticResource OnClick1}"/&gt;
                                  &lt;/EventTrigger&gt;
                                  &lt;EventTrigger RoutedEvent="Mouse.MouseLeave"&gt;
                                      &lt;BeginStoryboard x:Name="OnMouseLeave1_BeginStoryboard" Storyboard="{StaticResource OnMouseLeave1}"/&gt;
                                  &lt;/EventTrigger&gt;
                                  &lt;EventTrigger RoutedEvent="FrameworkElement.Loaded"/&gt;
                                  &lt;EventTrigger RoutedEvent="Mouse.MouseEnter"&gt;
                                      &lt;BeginStoryboard x:Name="OnMouseMove1_BeginStoryboard" Storyboard="{StaticResource OnMouseMove1}"/&gt;
                                  &lt;/EventTrigger&gt;
                                  &lt;Trigger Property="IsFocused" Value="True"/&gt;
                                  &lt;Trigger Property="IsDefaulted" Value="True"/&gt;
                                  &lt;Trigger Property="IsMouseOver" Value="True"/&gt;
                                  &lt;Trigger Property="IsPressed" Value="True"/&gt;
                                  &lt;Trigger Property="IsEnabled" Value="False"/&gt;
                              &lt;/ControlTemplate.Triggers&gt;
                          &lt;/ControlTemplate&gt;
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
  
                  &lt;Setter Property="Background"&gt;
                      &lt;Setter.Value&gt;
                          &lt;LinearGradientBrush EndPoint="0,1" StartPoint="0,0"&gt;
                              &lt;GradientStop Color="#FFF3F3F3" Offset="0"/&gt;
                              &lt;GradientStop Color="#FFEBEBEB" Offset="0.5"/&gt;
                              &lt;GradientStop Color="#FFDDDDDD" Offset="0.5"/&gt;
                              &lt;GradientStop Color="#E1CDCDCD" Offset="1"/&gt;
                          &lt;/LinearGradientBrush&gt;
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
              &lt;/Style&gt;
          &lt;/Window.Resources&gt;
      &lt;Window.Triggers&gt;
          &lt;EventTrigger RoutedEvent="FrameworkElement.Loaded"/&gt;
      &lt;/Window.Triggers&gt;
      &lt;Window.Background&gt;
          &lt;LinearGradientBrush EndPoint="0.484,0.543" StartPoint="0.478,0.009"&gt;
              &lt;GradientStop Color="Gray" Offset="1"/&gt;
              &lt;GradientStop Color="DarkGray" Offset="0"/&gt;
          &lt;/LinearGradientBrush&gt;
      &lt;/Window.Background&gt;
  
          &lt;Grid x:Name="LayoutRoot"&gt;
  
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="*"/&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;Canvas Height="282" HorizontalAlignment="Left" Margin="12,12,0,0" Name="canvas1" VerticalAlignment="Top" Width="343" /&gt;
      &lt;/Grid&gt;
  
  &lt;/Window&gt;
  </code></pre>