Advertisement
Guest User

WPF - Create Buttons in a Grid Programatically

a guest
Nov 28th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Class MainWindow
  2.  
  3.     Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
  4.         Dim counter As Integer = 1
  5.  
  6.         Dim rowDict As New Dictionary(Of Integer, RowDefinition)
  7.         Dim colDict As New Dictionary(Of Integer, ColumnDefinition)
  8.         Dim maxRows As Integer = rowSlider.Value
  9.         Dim maxCols As Integer = colSlider.Value
  10.  
  11.         rowDict.Clear()
  12.         colDict.Clear()
  13.         Panel.RowDefinitions.Clear()
  14.         Panel.ColumnDefinitions.Clear()
  15.         Panel.Children.Clear()
  16.         Panel.UpdateLayout()
  17.  
  18.         Panel.Width = xcontainer.ActualWidth - 8
  19.         Panel.Height = ycontainer.ActualHeight - 8
  20.  
  21.         For i As Integer = 0 To maxRows - 1
  22.  
  23.             If Not rowDict.ContainsKey(i) Then
  24.                 Dim rd As New RowDefinition()
  25.                 rd.Height = New GridLength(Panel.ActualHeight / maxRows)
  26.                 Panel.RowDefinitions.Add(rd)
  27.                 rowDict.Add(i, rd)
  28.             End If
  29.  
  30.             For j As Integer = 0 To maxCols - 1
  31.  
  32.                 If Not colDict.ContainsKey(j) Then
  33.                     Dim cd As New ColumnDefinition()
  34.                     cd.Width = New GridLength(Panel.ActualWidth / maxCols)
  35.                     Panel.ColumnDefinitions.Add(cd)
  36.                     colDict.Add(j, cd)
  37.                 End If
  38.  
  39.                 Dim b As Button = CreateButton(counter.ToString())
  40.  
  41.                 AddHandler b.Click, AddressOf DynButtonClicked
  42.  
  43.                 Grid.SetColumn(b, j)
  44.                 Grid.SetRow(b, i)
  45.  
  46.                 Panel.Children.Add(b)
  47.  
  48.                 counter += 1
  49.             Next
  50.         Next
  51.  
  52.         Panel.UpdateLayout()
  53.  
  54.     End Sub
  55.  
  56.     Private Function CreateButton(text As String)
  57.         Dim b As New Button
  58.         b.Tag = text
  59.         'b.Margin = New Thickness(2)
  60.         Dim cc As New ContentControl()
  61.         Dim vb As New Viewbox()
  62.         Dim tb As New TextBlock(New Run(text))
  63.         vb.Child = tb
  64.         cc.Content = vb
  65.         b.Content = cc
  66.         b.HorizontalAlignment = Windows.HorizontalAlignment.Stretch
  67.         b.VerticalAlignment = Windows.VerticalAlignment.Stretch
  68.         Return b
  69.     End Function
  70.  
  71.     Private Sub DynButtonClicked(sender As Object, e As RoutedEventArgs)
  72.         Try
  73.             source.Text = "You clicked " & sender.Tag.ToString()
  74.         Catch ex As Exception
  75.  
  76.         End Try
  77.     End Sub
  78.  
  79.     Private Sub Panel_SizeChanged(sender As Object, e As SizeChangedEventArgs)
  80.         If Not IsInitialized Then Return
  81.  
  82.         Panel.Width = xcontainer.ActualWidth - 8
  83.         Panel.Height = ycontainer.ActualHeight - 8
  84.  
  85.         Button_Click(doit, New RoutedEventArgs())
  86.  
  87.  
  88.     End Sub
  89.  
  90.     Private Sub rowSlider_ValueChanged(sender As Object, e As RoutedPropertyChangedEventArgs(Of Double)) Handles rowSlider.ValueChanged
  91.         If Not IsInitialized Then Return
  92.         Button_Click(doit, New RoutedEventArgs())
  93.  
  94.     End Sub
  95.  
  96.     Private Sub colSlider_ValueChanged(sender As Object, e As RoutedPropertyChangedEventArgs(Of Double)) Handles colSlider.ValueChanged
  97.         If Not IsInitialized Then Return
  98.         Button_Click(doit, New RoutedEventArgs())
  99.  
  100.     End Sub
  101.  
  102.     '<Window x:Class="MainWindow"
  103.     '    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  104.     '    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  105.     '    Title="MainWindow" Height="350" Width="525" SizeChanged="Panel_SizeChanged" >
  106.     '    <Grid Background="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
  107.     '        <Grid.ColumnDefinitions >
  108.     '            <ColumnDefinition x:Name="xcontainer"></ColumnDefinition>
  109.     '        </Grid.ColumnDefinitions>
  110.     '        <Grid.RowDefinitions>
  111.     '            <RowDefinition x:Name="ycontainer" Height="60*"></RowDefinition>
  112.     '            <RowDefinition Height="30*"/>
  113.     '            <RowDefinition Height="10*"/>
  114.     '        </Grid.RowDefinitions>
  115.     '        <Grid x:Name="Panel" Margin="4" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
  116.     '        <Grid Grid.Row="1">
  117.     '            <Grid.RowDefinitions>
  118.     '                <RowDefinition />
  119.     '                <RowDefinition />
  120.     '            </Grid.RowDefinitions>
  121.     '            <Grid.ColumnDefinitions>
  122.     '                <ColumnDefinition />
  123.     '                <ColumnDefinition />
  124.     '            </Grid.ColumnDefinitions>
  125.     '            <Button Margin="4" Grid.Column="1" Grid.RowSpan="2" Click="Button_Click" x:Name="doit" >
  126.     '                <ContentControl>
  127.     '                    <Viewbox>
  128.     '                        <TextBlock Text="Update" />
  129.     '                    </Viewbox>
  130.     '                </ContentControl>
  131.     '            </Button>
  132.     '            <Slider x:Name="rowSlider" Minimum="1" Maximum="10" Value="4" Grid.Row="0" Grid.Column="0" SmallChange="1" Cursor="SizeWE"  />
  133.     '            <Slider x:Name="colSlider" Minimum="1" Maximum="10" Value="4" Grid.Row="1" Grid.Column="0" SmallChange="1" Cursor="SizeWE" />
  134.     '        </Grid>
  135.     '        <Label Grid.Row="2" >
  136.     '            <ContentControl>
  137.     '                <Viewbox>
  138.     '                    <TextBlock Text="You Clicked:" x:Name="source"></TextBlock>
  139.     '                </Viewbox>
  140.     '            </ContentControl>
  141.     '        </Label>
  142.     '    </Grid>
  143.     '</Window>
  144.  
  145. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement