没有更多的显示的图像列表框,每次我选择一个项目图像、项目、更多、列表

2023-09-06 05:18:38 作者:凉笙

问题:

我的answer做一些从ListBoxSource结合ListBoxDisplay 但是当奇怪的事情发生了:

的选择和项目的取消选择工作正常,并显示完全所选项目的其他列表框名为ListBoxDetails的可是每次我选择的图像消失的项目,但选择的亮点依然,但没有更多的形象吧。(你仍然可以取消它,虽然即使没有更多的图像,因为屏幕空间仍然存在)

注意:我有ListBoxSource内没有其他控制(的SelectionMode =多),唯一的形象

code XAML:

 < Window.Resources>
    < D​​ataTemplate中X:关键=ItemTemplate中>
        < WrapPanel中高度=149方向=横向WIDTH =193>
            <图像的Horizo​​ntalAlignment =左的高度=128宽度=180保证金=0/>
        < / WrapPanel中>
    < / DataTemplate中>
    < ItemsPanelTemplate X:关键=ItemsPanelTemplate1>
        < StackPanel的方向=横向/>
    < / ItemsPanelTemplate>
    < ItemsPanelTemplate X:关键=ItemsPanelTemplate2>
        < UniformGrid X:名称=UniformGridImageList/>
    < / ItemsPanelTemplate>
< /Window.Resources>
<电网X:名称=LayoutRoot>
    < ListBox的X:名称=ListBoxSourceWIDTH =450保证金=9,3,442,178ItemsPanel ={DynamicResource ItemsPanelTemplate2}的SelectionMode =多D:LayoutOverrides =GridBox的Horizo​​ntalAlignment =左/>
    <列表框名称=ListBoxDisplay高度=659的Horizo​​ntalAlignment =右保证金=460,5,0,0VerticalAlignment =热门WIDTH =382的ItemsSource ={结合的ElementName = ListBoxSource,路径= SelectedItems}/>
    <按钮X:名称=buttonLoadImagesCONTENT =按钮的Horizo​​ntalAlignment =左的高度=51保证金=33,0,0,70VerticalAlignment =底部WIDTH =183风格={ DynamicResource ButtonStyle1}点击=buttonLoadImages_Click/>
    <按钮内容=清除高度=55的Horizo​​ntalAlignment =右保证金=0,717,442,0NAME =buttonClearListBoxVerticalAlignment =热门WIDTH =177点击=的button1_Click/>

< /网格>
 

code C#:

上面的声明:

 私人列表<图像> _imageList =新的List<图像>();
 
QQ不小心把好友列表里自己的头像设置没了

。 。

 私人无效buttonLoadImages_Click(对象发件人,System.Windows.RoutedEventArgs E)
    {

        this._imageList = GetImageList(@C:\用户\公用\图片\样品图片);


        的foreach(在this._imageList图像curImage)
        {

            ListBoxSource.Items.Add(curImage);

        }

    }

    #地区GetImageList方法

    私人列表<图像> GetImageList(字符串strPath中)
    {
        清单<图像>图像列表=新的List<图像>();
        字符串strFilePath =;

        如果(Directory.Exists(strPath的)==假)
        {
            的MessageBox.show(的String.Format({0}路径没有找到。,strPath中));
            返回图像列表;
        }

        尝试
        {


            DirectoryInfo的dirInfo =新的DirectoryInfo(strPath的);
            的FileInfo []文件= dirInfo.GetFiles(,SearchOption.AllDirectories* JPG。);


            的foreach(FileInfo的curFile中的文件)
            {


                strFilePath = curFile.FullName;

                图片curImage =新的图像();
                BitmapImage的bmpImage =新的BitmapImage();
                bmpImage.BeginInit();
                bmpImage.UriSource =新的URI(curFile.FullName,UriKind.Absolute);
                bmpImage.EndInit();

                curImage.Height = 140;
                curImage.Stretch = Stretch.Fill;

                curImage.Source = bmpImage;
                curImage.Margin =新厚度(10);

                imageList.Add(curImage);
            }

            如果(imageList.Count == 0)
                ;的MessageBox.show(的String.Format(,strPath的)无图像文件可以在找到{0})
        }
        赶上(例外前)
        {
            的MessageBox.show(的String.Format({0}  -  {1},ex.Message,strFilePath));
        }

        返回图像列表;
    }

    私人无效的button1_Click(对象发件人,RoutedEventArgs E)
    {
        this.listBoxSource.Items.Clear();
    }
 

解决方案

您正在创建图片 UI对象,并直接将其添加到您的列表框。

这意味着 SelectedItems 是一个图片 UI对象,因此列表框#2正试图设置它的产品以完全相同的图片对象的引用。这不是在WPF不允许的,因为UI对象只能有一个单亲家庭,但因为它是有约束力的错误,WPF是闭口不谈它(除了可能是一个警告)

我会建议让您的列表<图像> 名单,其中,串> ,其中包含的路径名图像,改变你的<图像> 在模板中使用的字符串作为它的源代码。另外,不要忘记设置 ItemTemplate中双方的列表框。

 < D​​ataTemplate中X:关键=ItemTemplate中>
    < WrapPanel中高度=149方向=横向WIDTH =193>
        <图像源={结合}的Horizo​​ntalAlignment =左的高度=128宽度=180保证金=0/>
    < / WrapPanel中>
< / DataTemplate中>
 

我做了一个快速测试,它一旦您填写您的列表框与字符串代替UI对象正常工作

PROBLEM:

I got the answer when doing some binding from ListBoxSource to ListBoxDisplay BUT weird thing happened:

The selecting and deselecting of items are working fine and displays exactly the selected items on the other ListBox named "ListBoxDetails BUT everytime I select an item the image is gone but selection highlights remain but has no more image in it. (You can still deselect it though even if no more image because the screenspace is still there)

NOTE: I have no other control inside the ListBoxSource (SelectionMode=Multiple) ONLY Image

CODE XAML:

    <Window.Resources>
    <DataTemplate x:Key="ItemTemplate">
        <WrapPanel Height="149" Orientation="Horizontal" Width="193">
            <Image HorizontalAlignment="Left" Height="128" Width="180" Margin="0"/>
        </WrapPanel>
    </DataTemplate>
    <ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
        <StackPanel Orientation="Horizontal"/>
    </ItemsPanelTemplate>
    <ItemsPanelTemplate x:Key="ItemsPanelTemplate2">
        <UniformGrid x:Name="UniformGridImageList"/>
    </ItemsPanelTemplate>
</Window.Resources>
<Grid x:Name="LayoutRoot">
    <ListBox x:Name="ListBoxSource" Width="450" Margin="9,3,442,178"  ItemsPanel="{DynamicResource ItemsPanelTemplate2}" SelectionMode="Multiple" d:LayoutOverrides="GridBox" HorizontalAlignment="Left" />     
    <ListBox Name="ListBoxDisplay" Height="659" HorizontalAlignment="Right" Margin="460,5,0,0"  VerticalAlignment="Top" Width="382" ItemsSource="{Binding ElementName=ListBoxSource, Path=SelectedItems}" />
    <Button x:Name="buttonLoadImages" Content="Button" HorizontalAlignment="Left" Height="51" Margin="33,0,0,70" VerticalAlignment="Bottom" Width="183" Style="{DynamicResource ButtonStyle1}" Click="buttonLoadImages_Click"/>
    <Button Content="Clear" Height="55" HorizontalAlignment="Right" Margin="0,717,442,0" Name="buttonClearListBox" VerticalAlignment="Top" Width="177" Click="button1_Click" />

</Grid>

CODE C#:

above declaration:

 private List<Image> _imageList = new List<Image>();

. . .

        private void buttonLoadImages_Click(object sender, System.Windows.RoutedEventArgs e)
    {

        this._imageList = GetImageList(@"C:\Users\Public\Pictures\Sample Pictures");


        foreach (Image curImage in this._imageList)
        {

            ListBoxSource.Items.Add(curImage);

        }

    }

    #region GetImageList Method

    private List<Image> GetImageList(string strPath)
    {
        List<Image> imageList = new List<Image>();
        string strFilePath = "";

        if (Directory.Exists(strPath) == false)
        {
            MessageBox.Show(string.Format("{0} path could not be found.", strPath));
            return imageList;
        }

        try
        {


            DirectoryInfo dirInfo = new DirectoryInfo(strPath);
            FileInfo[] files = dirInfo.GetFiles("*.jpg",SearchOption.AllDirectories);


            foreach (FileInfo curFile in files)
            {


                strFilePath = curFile.FullName;

                Image curImage = new Image();
                BitmapImage bmpImage = new BitmapImage();
                bmpImage.BeginInit();
                bmpImage.UriSource = new Uri(curFile.FullName, UriKind.Absolute);
                bmpImage.EndInit();

                curImage.Height = 140;
                curImage.Stretch = Stretch.Fill;

                curImage.Source = bmpImage;
                curImage.Margin = new Thickness(10);

                imageList.Add(curImage);
            }

            if (imageList.Count == 0)
                MessageBox.Show(string.Format("No image files could be found in {0}", strPath));
        }
        catch (Exception ex)
        {
            MessageBox.Show(string.Format("{0}-{1}", ex.Message, strFilePath));
        }

        return imageList;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        this.listBoxSource.Items.Clear();
    }

解决方案

You are creating Image UI objects and adding them directly to your ListBox.

This means that the SelectedItems is an Image UI object, so ListBox #2 is trying to set it's Items to the exact same Image object reference. This isn't allowed in WPF because UI objects can only have a single parent, however because it is a Binding error, WPF is silent about it (except for probably a warning)

I would recommend making your List<Image> into a List<string> which contains the path name for the image, and changing your <Image> in the template to use that string as it's Source. Also, don't forget to set the ItemTemplate on both your ListBoxes.

<DataTemplate x:Key="ItemTemplate">
    <WrapPanel Height="149" Orientation="Horizontal" Width="193">
        <Image Source="{Binding }" HorizontalAlignment="Left" Height="128" Width="180" Margin="0" />
    </WrapPanel>
</DataTemplate>

I did a quick test and it works fine once you fill your ListBoxes with Strings instead of UI objects

 
精彩推荐
图片推荐