获取所选列表框项目的名称(WPF C#)?

发布于 2024-11-02 17:23:18 字数 2432 浏览 4 评论 0原文

全部。我有一个应用程序可以扫描图片文件夹并在列表框中显示图像及其名称。每个图像和图像名称(显示在图像旁边的文本块中)都存储在列表框内的水平堆栈面板中。

我整个下午都在尝试找到一种当用户在列表框中选择图像名称时在文本框中显示图像名称的方法。听起来很简单,我确信确实如此,但我似乎无法让它发挥作用。

谁能指出我正确的方向以及执行此操作的最佳方法?谢谢。

这是我的 xaml(如果有帮助的话):

<Grid>

    <ListBox ItemsSource="{Binding AllImages}" Margin="0,0,262,0" Name="listBox1" MouseLeftButtonDown="listBox1_MouseLeftButtonDown">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding Image}" Width="50" Height="50" Margin="6"/>
                    <TextBlock Text="{Binding Name}" Margin="6" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>        
    <TextBox Height="23" HorizontalAlignment="Left" Margin="265,148,0,0" Name="textBox1" VerticalAlignment="Top" Width="198" />
</Grid>

以及我后面的代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    public class MyImage
    {
        private ImageSource _image;
        private string _name;

        public MyImage(ImageSource image, string name)
        {
            _image = image;
            _name = name;
        }

        public override string ToString()
        {
            return _name;
        }

        public ImageSource Image
        {
            get { return _image; }
        }

        public string Name
        {
            get { return _name; }
        }
    }

    public List<MyImage> AllImages
    {
        get
        {
            List<MyImage> result = new List<MyImage>();
            string filePath = @"D:\Projects\Visual Studio 2010\WpfApplication5\WpfApplication5\bin\Debug\ImageFolder";
            string[] files = Directory.GetFiles(filePath);
            foreach (string filename in files)
            {
                try
                {
                    result.Add(
                    new MyImage(
                    new BitmapImage(
                    new Uri(filename)),
                    System.IO.Path.GetFileNameWithoutExtension(filename)));                        
                }
                catch { }
            }
            return result;
        }
    }        

}

all. I have an app that scans a picture folder and displays the images along with their names in a listbox. Each image and image name (displayed in a textblock next to the image) is stored in a horizontal stackpanel inside the listbox.

I've been trying all afternoon to find a way of displaying the image name in a textbox when the user selects it in the listbox. Sounds very simple, and I'm sure it is, but I can't seem to get it to work.

Can anyone point me in the right direction as to the best way of doing this? Thanks.

Here is my xaml if it helps:

<Grid>

    <ListBox ItemsSource="{Binding AllImages}" Margin="0,0,262,0" Name="listBox1" MouseLeftButtonDown="listBox1_MouseLeftButtonDown">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding Image}" Width="50" Height="50" Margin="6"/>
                    <TextBlock Text="{Binding Name}" Margin="6" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>        
    <TextBox Height="23" HorizontalAlignment="Left" Margin="265,148,0,0" Name="textBox1" VerticalAlignment="Top" Width="198" />
</Grid>

And my code behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    public class MyImage
    {
        private ImageSource _image;
        private string _name;

        public MyImage(ImageSource image, string name)
        {
            _image = image;
            _name = name;
        }

        public override string ToString()
        {
            return _name;
        }

        public ImageSource Image
        {
            get { return _image; }
        }

        public string Name
        {
            get { return _name; }
        }
    }

    public List<MyImage> AllImages
    {
        get
        {
            List<MyImage> result = new List<MyImage>();
            string filePath = @"D:\Projects\Visual Studio 2010\WpfApplication5\WpfApplication5\bin\Debug\ImageFolder";
            string[] files = Directory.GetFiles(filePath);
            foreach (string filename in files)
            {
                try
                {
                    result.Add(
                    new MyImage(
                    new BitmapImage(
                    new Uri(filename)),
                    System.IO.Path.GetFileNameWithoutExtension(filename)));                        
                }
                catch { }
            }
            return result;
        }
    }        

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就像说晚安 2024-11-09 17:23:18

看看这个问题。

如何绑定使用 TwoWay 模式将 Listview SelectedItem 转换为文本框?

在您的情况下使用

<TextBox Height="23" 
  HorizontalAlignment="Left" 
  Margin="265,148,0,0" 
  Name="textBox1" 
  VerticalAlignment="Top" Width="198" 
  Text="{Binding SelectedItem.Name, ElementName=listBox1}"/>

Take a look at this question.

How do I bind a Listview SelectedItem to a Textbox using the TwoWay mode?

In your case use

<TextBox Height="23" 
  HorizontalAlignment="Left" 
  Margin="265,148,0,0" 
  Name="textBox1" 
  VerticalAlignment="Top" Width="198" 
  Text="{Binding SelectedItem.Name, ElementName=listBox1}"/>
迎风吟唱 2024-11-09 17:23:18

要从代码中检索所选图像,您至少有 3 个选项(我假设您的图像由类 ImageItem 表示)

  • IsSynchronizedWithCurrentItem 上将 IsSynchronizedWithCurrentItem 设置为 true code>ListBox,并使用以下代码检索所选项目:

    ICollectionView 视图 = CollectionViewSource(AllImages);
    ImageItem selectedImage = (ImageItem)view.CurrentItem;
    
  • 绑定 SelectedItem ListBoxDataContext 中的属性:

    
    
  • 访问 SelectedItem 属性直接来自代码隐藏:

    ImageItem selectedImage = (ImageItem)listBox1.SelectedItem;
    

当然,如果您只想在TextBlock中显示名称,您可以使用拉塞尔·特洛伊韦斯特的解决方案

To retrieve the selected image from code, you have at least 3 options (I assume your images are represented by a class ImageItem)

  • Set IsSynchronizedWithCurrentItem to true on your ListBox, and use the following code to retrieve the selected item:

    ICollectionView view = CollectionViewSource(AllImages);
    ImageItem selectedImage = (ImageItem)view.CurrentItem;
    
  • Bind the SelectedItem of the ListBox to a property in your DataContext:

    <ListBox .... SelectedItem="{Binding SelectedImage}">
    
  • Access the SelectedItem property directly from code-behind:

    ImageItem selectedImage = (ImageItem)listBox1.SelectedItem;
    

Of course, if you just want to show the name in a TextBlock, you can use Russell Troywest's solution

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文