通过代码将图像添加到Wrappanel

时间:2016-07-06 13:03:23

标签: c# wpf image wrappanel

我的WPF中有一个Wrappanel,如:

<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>

我的c#-Code看起来像:

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();

编译时我没有收到任何错误,但我的报道仍然空白,我无法弄清楚原因。

我在另一个页面上使用几乎相同的代码来生成Checkbox-Controls,它运行正常。

有谁能请我暗示我做错了什么?

2 个答案:

答案 0 :(得分:2)

如果列 nr 是一个数字,则应该修复您的查询:

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

您将其视为字符串而不是数字(您在查询中使用'字符)。

答案 1 :(得分:0)

我的第一个猜测是,您的初始 var arrayOfStrings: [String]? do { // This solution assumes you've got the file in your bundle if let path = Bundle.main.path(forResource: "YourTextFilename", ofType: "txt"){ let data = try String(contentsOfFile:path, encoding: String.Encoding.utf8) arrayOfStrings = data.components(separatedBy: "\n") print(arrayOfStrings) } } catch let err as NSError { // do something with Error print(err) } 应该被删除

Read()