将图像绑定到wpf中的列表框

时间:2013-07-30 09:07:06

标签: c# wpf

我在列表框中得到空白图像,图像源没有绑定我认为......

.xaml代码:

                <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel FlowDirection="LeftToRight" ItemWidth="120" ItemHeight="120"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Button x:Name="b" Click="b_Click_1" Width="120" Height="120" >
                        <Image HorizontalAlignment="Left" 
                        Margin="-10,-10,-10,-10" 
                        x:Name="image1" 
                        Stretch="Fill" 
                        VerticalAlignment="Top" Source="{Binding}"
                    />
                    </Button>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

` .cs代码:

         DirectoryInfo dir = new DirectoryInfo(@"C:\Users\sandeep ams\Documents\Images");
                FileInfo[] file = dir.GetFiles();
                ArrayList list = new ArrayList();
                foreach (FileInfo file2 in file)
                {
                    if (file2.Extension == ".JPG" || file2.Extension == ".JPEG" || file2.Extension == ".GIF" || file2.Extension == ".PNG")
                    {
                        list.Add(file2);
                    }
                }
                listBox.ItemsSource = list;

这里我试图将图像绑定到列表框图像源不绑定它在列表框中显示空图像。

1 个答案:

答案 0 :(得分:1)

你可以这样做:

foreach (FileInfo fileInfo in files)
{
    if (fileInfo.Extension.Equals(".JPG", StringComparison.InvariantCultureIgnoreCase) ||
        fileInfo.Extension.Equals(".JPEG", StringComparison.InvariantCultureIgnoreCase) ||
        fileInfo.Extension.Equals(".GIF", StringComparison.InvariantCultureIgnoreCase) ||
        fileInfo.Extension.Equals(".PNG", StringComparison.InvariantCultureIgnoreCase))
    {
        list.Add(fileInfo.FullName);
    }
}