Hey all i am trying to add a label to my grid using the following code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
Dim dynamicLabel As New Label()
Dim g As New Grid
dynamicLabel.Name = "NewLabel"
dynamicLabel.Content = "TEST"
dynamicLabel.Width = 240
dynamicLabel.Height = 30
dynamicLabel.Margin = New Thickness(0, 21, 0, 0)
dynamicLabel.Foreground = New SolidColorBrush(Colors.White)
dynamicLabel.Background = New SolidColorBrush(Colors.Black)
Grid.SetRow(dynamicLabel, 0)
Grid.SetColumn(dynamicLabel, 6)
g.Children.Add(dynamicLabel)
End Sub
However, i never see anything on the grid after i push the button... what am i missing?
Select the column grid you want to label. Click Column Grid tab Label panel Label. Specify the grid location for labels: If you want to…
The Grid class in WPF represents a Grid control. The following code snippet creates a Grid control, sets its width, horizontal alignment, vertical alignment, show grid lines, and background color. Grid DynamicGrid = new Grid();
First Method: By typing XAML CodeRowDefinitions property and a ColumnDefinition Element for each column inside the Grid. ColumnDefinitions property. By default, GridLines are invisible. For showing the GridLines, set the ShowGridLines property of the Grid to True.
I corrected this by naming the other grid. I never had the grid named and therefore thats why the .Children.Add never would add to the grid. I tried mainGrid.Children.Add (mainGrid is the name of my parent grid) and it would always throw an error because it was the grid inside that it was needing for this part.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) //Handles Button1.Click
Dim dynamicLabel As new Label();
dynamicLabel.Name = "NewLabel";
dynamicLabel.Content = "TEST";
dynamicLabel.Width = 240;
dynamicLabel.Height = 30;
dynamicLabel.Margin = new Thickness(0, 21, 0, 0);
dynamicLabel.Foreground = new SolidColorBrush(Colors.White);
dynamicLabel.Background = new SolidColorBrush(Colors.Black);
Grid.SetRow(dynamicLabel, 0);
Grid.SetColumn(dynamicLabel, 0);
childGrid.Children.Add(dynamicLabel); //'<-changed to grid name in XAML properties
End Sub
If you need to add a button or a Label (just change Button to Label) to a Grid, you can use this:
internal void FillbtnSubCat(Grid grid)
{
var myDefinition = new ColumnDefinition();
var myButton = new Button();
Grid.SetColumn(myButton, count);
myButton.Margin = new Thickness(5, 10, 5, 25);
myButton.MinWidth = 30;
myButton.Content = count;
myButton.Click+= new RoutedEventHandler(Click1);
myDefinition.Width = new GridLength(68);
grid.ColumnDefinitions.Add(myDefinition);
grid.Children.Add(myButton);
count++;
}
void Click1(object sender, RoutedEventArgs e)
{
var myButton = sender as Button;
MessageBox.Show(myButton.GetHashCode().ToString());
}
and XAML
<ListView Grid.Column="0" Margin="10,10,5,5" Grid.Row="1" Grid.ColumnSpan="2">
<Grid Name="Grid1" Height="100" Width="auto">
</Grid>
</ListView>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With