Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Class MainWindow
- Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
- Dim counter As Integer = 1
- Dim rowDict As New Dictionary(Of Integer, RowDefinition)
- Dim colDict As New Dictionary(Of Integer, ColumnDefinition)
- Dim maxRows As Integer = rowSlider.Value
- Dim maxCols As Integer = colSlider.Value
- rowDict.Clear()
- colDict.Clear()
- Panel.RowDefinitions.Clear()
- Panel.ColumnDefinitions.Clear()
- Panel.Children.Clear()
- Panel.UpdateLayout()
- Panel.Width = xcontainer.ActualWidth - 8
- Panel.Height = ycontainer.ActualHeight - 8
- For i As Integer = 0 To maxRows - 1
- If Not rowDict.ContainsKey(i) Then
- Dim rd As New RowDefinition()
- rd.Height = New GridLength(Panel.ActualHeight / maxRows)
- Panel.RowDefinitions.Add(rd)
- rowDict.Add(i, rd)
- End If
- For j As Integer = 0 To maxCols - 1
- If Not colDict.ContainsKey(j) Then
- Dim cd As New ColumnDefinition()
- cd.Width = New GridLength(Panel.ActualWidth / maxCols)
- Panel.ColumnDefinitions.Add(cd)
- colDict.Add(j, cd)
- End If
- Dim b As Button = CreateButton(counter.ToString())
- AddHandler b.Click, AddressOf DynButtonClicked
- Grid.SetColumn(b, j)
- Grid.SetRow(b, i)
- Panel.Children.Add(b)
- counter += 1
- Next
- Next
- Panel.UpdateLayout()
- End Sub
- Private Function CreateButton(text As String)
- Dim b As New Button
- b.Tag = text
- 'b.Margin = New Thickness(2)
- Dim cc As New ContentControl()
- Dim vb As New Viewbox()
- Dim tb As New TextBlock(New Run(text))
- vb.Child = tb
- cc.Content = vb
- b.Content = cc
- b.HorizontalAlignment = Windows.HorizontalAlignment.Stretch
- b.VerticalAlignment = Windows.VerticalAlignment.Stretch
- Return b
- End Function
- Private Sub DynButtonClicked(sender As Object, e As RoutedEventArgs)
- Try
- source.Text = "You clicked " & sender.Tag.ToString()
- Catch ex As Exception
- End Try
- End Sub
- Private Sub Panel_SizeChanged(sender As Object, e As SizeChangedEventArgs)
- If Not IsInitialized Then Return
- Panel.Width = xcontainer.ActualWidth - 8
- Panel.Height = ycontainer.ActualHeight - 8
- Button_Click(doit, New RoutedEventArgs())
- End Sub
- Private Sub rowSlider_ValueChanged(sender As Object, e As RoutedPropertyChangedEventArgs(Of Double)) Handles rowSlider.ValueChanged
- If Not IsInitialized Then Return
- Button_Click(doit, New RoutedEventArgs())
- End Sub
- Private Sub colSlider_ValueChanged(sender As Object, e As RoutedPropertyChangedEventArgs(Of Double)) Handles colSlider.ValueChanged
- If Not IsInitialized Then Return
- Button_Click(doit, New RoutedEventArgs())
- End Sub
- '<Window x:Class="MainWindow"
- ' xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- ' xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- ' Title="MainWindow" Height="350" Width="525" SizeChanged="Panel_SizeChanged" >
- ' <Grid Background="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
- ' <Grid.ColumnDefinitions >
- ' <ColumnDefinition x:Name="xcontainer"></ColumnDefinition>
- ' </Grid.ColumnDefinitions>
- ' <Grid.RowDefinitions>
- ' <RowDefinition x:Name="ycontainer" Height="60*"></RowDefinition>
- ' <RowDefinition Height="30*"/>
- ' <RowDefinition Height="10*"/>
- ' </Grid.RowDefinitions>
- ' <Grid x:Name="Panel" Margin="4" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
- ' <Grid Grid.Row="1">
- ' <Grid.RowDefinitions>
- ' <RowDefinition />
- ' <RowDefinition />
- ' </Grid.RowDefinitions>
- ' <Grid.ColumnDefinitions>
- ' <ColumnDefinition />
- ' <ColumnDefinition />
- ' </Grid.ColumnDefinitions>
- ' <Button Margin="4" Grid.Column="1" Grid.RowSpan="2" Click="Button_Click" x:Name="doit" >
- ' <ContentControl>
- ' <Viewbox>
- ' <TextBlock Text="Update" />
- ' </Viewbox>
- ' </ContentControl>
- ' </Button>
- ' <Slider x:Name="rowSlider" Minimum="1" Maximum="10" Value="4" Grid.Row="0" Grid.Column="0" SmallChange="1" Cursor="SizeWE" />
- ' <Slider x:Name="colSlider" Minimum="1" Maximum="10" Value="4" Grid.Row="1" Grid.Column="0" SmallChange="1" Cursor="SizeWE" />
- ' </Grid>
- ' <Label Grid.Row="2" >
- ' <ContentControl>
- ' <Viewbox>
- ' <TextBlock Text="You Clicked:" x:Name="source"></TextBlock>
- ' </Viewbox>
- ' </ContentControl>
- ' </Label>
- ' </Grid>
- '</Window>
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement