0

I create images dynamically, and insert column and row position values, but it doesn't show the pictures in the xaml.

My code is:

XAML:

<UserControl x:Class="DesignPanelSimulator.ImageFileTransferView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:DesignPanelSimulator"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<Grid Background="Red">
    <Grid x:Name="showImages" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
    </Grid>
</Grid>
</UserControl>

The delegate is activated and it Manages to get to the function and it runs

but in the xaml I don't see the pictures

in the xaml.cs:

    public delegate string FilterDelegate();

    public ImageFileTransferView()
    {
        InitializeComponent();
        DataContext =new ImageFileTransferViewModel();
        ListFromFolderToFileTransferViewModel.sendTheUrlForImage += ShowImage;
    }

    public void ShowImage(string url)
    {         
        string[] jpegFiles = Directory.GetFiles(url, "*.jpeg");    

        if (jpegFiles.Length > 0)
        {
            foreach (var image in jpegFiles)
            {
                Image ImageViewer = new Image();
                Grid.SetRow(showImages, x);
                Grid.SetColumn(showImages, y);
                string strUri2 = String.Format(image);
                ImageViewer.Source = new BitmapImage(new Uri(strUri2));
                showImages.Children.Add(ImageViewer);
                x += 2;

                if (x == 6)
                {
                    x = 0;
                    y = 2;
                }
            }
        }
    }

Thank you

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Lior Lev
  • 143
  • 7
  • Does `ShowImage()` event handler gets called? Set breakpoint on the first line to check. You have to [init](https://stackoverflow.com/a/530382/1997232) `BitmapImage`. *"The delegate is activated in another class"* - this is suspecious, you didn't show enough to say for sure, but it could be also a problem there. – Sinatr Jan 27 '20 at 11:01
  • `Grid.SetRow(showImages, x);` is nonsense. You certainly wanted to write `Grid.SetRow(ImageViewer, x);` – Clemens Jan 27 '20 at 11:25
  • Besides that, it would make *a lot more* sense to use an ItemsControl with a UniformGrid as ItemsPanel, which has its ItemsSource property assigned to a collection of BitmapImages, or image file path strings – Clemens Jan 27 '20 at 11:26

0 Answers0