Search code examples
c#wpfimagewrappanel

Adding Image to Wrappanel by code


I've got a a Wrappanel in my WPF like:

<DockPanel x:Name="dockpnlCollection_Covers" HorizontalAlignment="Center" Grid.Row="0" Grid.Column="0" LastChildFill="False" Margin="0" >
            <WrapPanel x:Name="wpnlCollection_Covers" Orientation="Vertical" MinWidth="520" VerticalAlignment="Top" HorizontalAlignment="Left" Height="Auto" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ScrollViewer}}, Path=ActualWidth}"></WrapPanel>
        </DockPanel>

My c#-Code looks like:

for (int x = 0; x <= nmbr; x++)
            {
                cmddb.CommandText = "SELECT cover FROM movies Where nr = '" + x + "'";
                dbreader.Read();
                while (dbreader.Read())
                {
                    coverpath = (string)dbreader["cover"];
                    System.Windows.Controls.Image newIMG = new System.Windows.Controls.Image();
                    newIMG.Margin = new System.Windows.Thickness { Left = 6, Top = 5, Right = 6, Bottom = 5 };
                    newIMG.Source = new BitmapImage(new Uri(coverpath));
                    wpnlCollection_Covers.Children.Add(newIMG);
                }
            }

            dbreader.Close();

I don't get any errors while compiling but my wrappanel stays blank and I can't figure out why.

I'm using nearly the same code on another page to generate Checkbox-Controls and it works just fine.

Could anyone please give me a hint what I'm doing wrong?


Solution

  • If column nr is a number this should fix your query:

    cmddb.CommandText = "SELECT cover FROM movies Where nr = " + x.ToString();
    

    You are treating it as a string and not as a number (you are using ' character into your query).