在ListBox中显示图像

时间:2014-04-07 07:59:43

标签: c# wpf binding listbox

我知道网上有很多这样的问题,但请相信我,我花了很多时间,但仍然没有成功,我真的很乐意为你提供帮助!

我在运行时加载各种图像,我想在列表框中显示它们(小图像,然后用户应点击其中一个并以实际尺寸显示它)。

我的代码是:

public partial class MainWindow : Window
{
    int imageNumber = 0;
    public List<String> ImagePath = new List<String>();

    public MainWindow()
    {
        InitializeComponent();
        lb_Images.ItemsSource = ImagePath;
    }

    private void bu_addImage_Click(object sender, RoutedEventArgs e)
    {
        addImageToListBox();
    }

    private void addImageToListBox()
    {
        imageNumber++;
        if (imageNumber == 4) imageNumber = 0;
        string directoryPath = AppDomain.CurrentDomain.BaseDirectory;

        // load input image
        string ImageFilename = directoryPath + "img";
        ImageFilename += imageNumber.ToString();
        ImageFilename += ".jpg";

        ImagePath.Add(ImageFilename);
    }
}

和xaml是:

<Window x:Class="forQuestionWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="216" Width="519">

    <Window.Resources>
        <DataTemplate x:Key="ImageGalleryDataTemplate">
            <Grid>
                <Border BorderBrush="#FFFF9800" BorderThickness="1"  Width="120" Height="120" Padding="5" Margin="5" CornerRadius="6">
                    <!--Bind Image Path in Image Control-->
                    <Image Source="{Binding ImagePath}" Stretch="Fill"  HorizontalAlignment="Center">
                        <!--View Large Image on Image Control Tooltip-->
                        <Image.ToolTip>
                            <Grid>
                                <Image Source="{Binding ImagePath}" Stretch="Fill" HorizontalAlignment="Center" Height="200" Width="200"></Image>
                            </Grid>
                        </Image.ToolTip>
                    </Image>
                </Border>
            </Grid>
        </DataTemplate>

        <ItemsPanelTemplate x:Key="ImageGalleryItemsPanelTemplate">
            <!--Display Images on UniformGrid Panel-->
            <UniformGrid Rows="1" Columns="25" HorizontalAlignment="Center" VerticalAlignment="Stretch"/>
        </ItemsPanelTemplate>
    </Window.Resources>

    <Grid>
        <Canvas Height="177" HorizontalAlignment="Left" Name="canvas1" VerticalAlignment="Top" Width="497">

            <ListBox Canvas.Left="6" Canvas.Top="5" Height="166" Name="lb_Images" Width="441"
                     BorderBrush="{x:Null}" DataContext="{Binding Source={StaticResource ImageGalleryDataTemplate}}"
                     ItemsSource="{Binding Source={StaticResource ImageGalleryItemsPanelTemplate}}">
            </ListBox>

            <Button Canvas.Left="453" Canvas.Top="26" Content="Add" Height="64" Name="bu_addImage" Width="38" Click="bu_addImage_Click" />
        </Canvas>
    </Grid>
</Window>

我知道当我将图像路径添加到列表时列表框已更新,因为如果我调试我在lb_Images.items下找到了一些项目,但我没有显示任何内容。 我很乐意为你提供帮助!谢谢!!

1 个答案:

答案 0 :(得分:2)

<强> Some notes

  • ListBox的DataContext没有必要,然后设置ItemSource。而不是设置ItemTemplate

  • 在DataTemplate中删除{Binding ImagePath},而不是写{Binding},因为在这种情况下,DataTemplate的元素会继承DataContext

  • 当您向ListBox.Items添加新项目时,您必须致电ListBox.Items.Refresh()或使用ObservableCollection<T>,因为:

  

ObservableCollection表示动态数据集合,provides notifications在添加,删除项目或刷新整个列表时{。}}。

试试这个例子:

<强> XAML

<Window.Resources>
    <DataTemplate x:Key="ImageGalleryDataTemplate">
        <Grid>
            <Border BorderBrush="#FFFF9800" BorderThickness="1"  Width="120" Height="120" Padding="5" Margin="5" CornerRadius="6">
                <Image Source="{Binding}" Stretch="Fill"  HorizontalAlignment="Center">
                    <Image.ToolTip>
                        <Grid>
                            <Image Source="{Binding}" Stretch="Fill" HorizontalAlignment="Center" Height="200" Width="200" />
                        </Grid>
                    </Image.ToolTip>
                </Image>
            </Border>
        </Grid>
    </DataTemplate>

    <ItemsPanelTemplate x:Key="ImageGalleryItemsPanelTemplate">
        <UniformGrid Rows="1" Columns="25" HorizontalAlignment="Center" VerticalAlignment="Stretch"/>
    </ItemsPanelTemplate>
</Window.Resources>

<Grid>
    <Canvas Height="177" HorizontalAlignment="Left" Name="canvas1" VerticalAlignment="Top" Width="497">

        <ListBox Canvas.Left="6" Canvas.Top="5" Height="166" Name="lb_Images" Width="441"  
                 ItemTemplate="{StaticResource ImageGalleryDataTemplate}"
                 ItemsSource="{Binding Path=ImagePath}">
        </ListBox>

        <Button Canvas.Left="453" Canvas.Top="26" Content="Add" Height="64" Name="bu_addImage" Width="38" Click="bu_addImage_Click" />
    </Canvas>
</Grid>

<强> Code-behind

public partial class MainWindow : Window
{
    int imageNumber = 0;
    public List<String> ImagePath = new List<String>();

    public MainWindow()
    {
        InitializeComponent();
        lb_Images.ItemsSource = ImagePath;
    }

    private void bu_addImage_Click(object sender, RoutedEventArgs e)
    {
        addImageToListBox();
    }

    private void addImageToListBox()
    {
        imageNumber++;
        if (imageNumber == 4) imageNumber = 0;
        string directoryPath = AppDomain.CurrentDomain.BaseDirectory;

        // load input image
        string ImageFilename = directoryPath + "img";
        ImageFilename += imageNumber.ToString();
        ImageFilename += ".jpg";

        ImagePath.Add(ImageFilename); 

        lb_Images.Items.Refresh();
    }
}