Column loaded event to set margin of children

50 views Asked by At

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);
    }
1

There are 1 answers

3
Timo Salomäki On

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.