0

I'm trying to dynamically set the margin of a textblock so that it's placed in the center of its parent column after said column has loaded but I'm getting the error Method name expected when I call my SetManga method.

    public void GetManga(List<Manga> mangaList)
    {
        stkPnlImages.Children.Clear();
        Grid imagesGrid = new Grid();
        imagesGrid.HorizontalAlignment = HorizontalAlignment.Left;
        imagesGrid.ColumnDefinitions.Add(new ColumnDefinition());
        imagesGrid.ColumnDefinitions.Add(new ColumnDefinition());
        stkPnlImages.Children.Add(imagesGrid);


        for (int i = 0; i < mangaList.Count; i++)
        {
            imagesGrid.RowDefinitions.Add(new RowDefinition());

            Image image = new Image(); 
            image.Source = new BitmapImage(new Uri(@"Covers\" + mangaList[i].SourceImage, UriKind.Relative));
            image.Height = 200;
            image.Margin = new Thickness(20);

            SolidColorBrush grayBrush = new SolidColorBrush() { Color = Colors.White };
            Rectangle imageBackground = new Rectangle() { Height = image.Height + 20, Width = image.Width + 20, Fill = grayBrush };
            imageBackground.Margin = new Thickness(10);

            TextBlock mangaName = new TextBlock() { Text = mangaList[i].Name, FontSize = 10, FontFamily = new FontFamily("Century Gothic")};

            Grid.SetRow(imageBackground, i);
            Grid.SetColumn(imageBackground, 0);
            Grid.SetRow(image, i);
            Grid.SetColumn(image, 0);
            Grid.SetRow(mangaName, i);
            Grid.SetColumn(mangaName, 1);

            image.MouseLeftButtonDown += new MouseButtonEventHandler(this.Image_Click);
            imagesGrid.Children.Add(imageBackground);
            imagesGrid.Children.Add(image);
            imagesGrid.Children.Add(mangaName);

            imagesGrid.ColumnDefinitions[1].Loaded += new RoutedEventHandler( SetMargin(mangaName, imagesGrid) );
        }
    }

    void SetMargin(TextBlock txtBlkMangaName, Grid grdImages)
    {
        txtBlkMangaName.Margin = new Thickness(grdImages.ColumnDefinitions[1].ActualWidth / 2 - txtBlkMangaName.Width / 2, 0, 0, 0);
    }
HyperPXLZ
  • 15
  • 5

1 Answers1

0

As the error says, RoutedEventHandler constructor expects a method name with a certain signature.

What you could try is to add mangaName to the Tag property of the imagesGrid, and then retrieve it within the SetMargin method. Also, SetMargin needs to have a specific signature for it to work as the RoutedEventHandler.

Change this:

imagesGrid.ColumnDefinitions[1].Loaded += new RoutedEventHandler( SetMargin(mangaName, imagesGrid) );

to this:

imagesGrid.Tag = mangaName;
imagesGrid.ColumnDefinitions[1].Loaded += new RoutedEventHandler(SetMargin);

and this:

void SetMargin(TextBlock txtBlkMangaName, Grid grdImages)
{
    txtBlkMangaName.Margin = new Thickness(grdImages.ColumnDefinitions[1].ActualWidth / 2 - txtBlkMangaName.Width / 2, 0, 0, 0);
}

to this:

void SetMargin(object sender, RoutedEventArgs e)
{
    var imagesGrid = sender as Grid;
    if( imagesGrid == null )
        return;

    var textBlock = imagesGrid.Tag as TextBlock;
    if( textBlock == null )
        return;

    textBlock.Margin = new Thickness(imagesGrid.ColumnDefinitions[1].ActualWidth / 2 - textBlock.Width / 2, 0, 0, 0);
}

As you can now see, the Grid is actually the sender for this event so you can easily retrieve it. Also, you can get a reference to the TextBlock that we stored in the Tag property earlier. From there, you just continue as normal and can change properties on each element.

Timo Salomäki
  • 7,099
  • 3
  • 25
  • 40
  • At `sender.Tag`, I'm getting the error **'object' does not contain definition for 'Tag'**. – HyperPXLZ Feb 09 '20 at 11:26
  • Right, my mistake. You need to get the Tag property of the `imagesGrid` variable that is already cast to Grid. I fixed the example. – Timo Salomäki Feb 09 '20 at 13:23
  • Sorry for the late reply but no, unfortunately, it didn't work. Everything looks right but it still doesn't position the `Textblock` the way I want. It's not really important in the end, just thought it would look nice. Thanks for the help though. – HyperPXLZ Feb 11 '20 at 15:06