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

获取所选列表框项目的名称(WPF C#)?,wpf,listbox,selecteditem,Wpf,Listbox,Selecteditem,全部。我有一个应用程序,可以扫描图片文件夹,并在列表框中显示图片及其名称。每个图像和图像名称(显示在图像旁边的文本块中)存储在列表框内的水平堆栈面板中 我整个下午都在试图找到一种方法,当用户在列表框中选择图像名称时,在文本框中显示图像名称。听起来很简单,我肯定是的,但我似乎无法让它发挥作用 有人能给我指出正确的方向,告诉我最好的方法吗?谢谢 以下是我的xaml(如果有帮助): <Grid> <ListBox ItemsSource="{Binding AllImage

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

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

有人能给我指出正确的方向,告诉我最好的方法吗?谢谢

以下是我的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;
        }
    }        

}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
DataContext=this;
}
公共类MyImage
{
私有图像源_图像;
私有字符串\u名称;
公共MyImage(图像源图像,字符串名称)
{
_图像=图像;
_名称=名称;
}
公共重写字符串ToString()
{
返回_name;
}
公共图像源图像
{
获取{return\u image;}
}
公共字符串名
{
获取{return\u name;}
}
}
公共列表所有图像
{
收到
{
列表结果=新列表();
字符串文件路径=@“D:\Projects\visualstudio 2010\WpfApplication5\WpfApplication5\bin\Debug\ImageFolder”;
string[]files=Directory.GetFiles(filePath);
foreach(文件中的字符串文件名)
{
尝试
{
结果。添加(
新MyImage(
新位图图像(
新Uri(文件名)),
System.IO.Path.GetFileNameWithoutExtension(filename));
}
捕获{}
}
返回结果;
}
}        
}

看看这个问题

在你的情况下使用

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

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

  • 列表框
    上将
    IsSynchronizedWithCurrentItem
    设置为true,并使用以下代码检索所选项目:

    ICollectionView view = CollectionViewSource(AllImages);
    ImageItem selectedImage = (ImageItem)view.CurrentItem;
    
  • 列表框的
    SelectedItem
    绑定到
    DataContext
    中的属性:

    <ListBox .... SelectedItem="{Binding SelectedImage}">
    

当然,如果您只想在
文本块中显示名称,您可以使用Russell Troywest的解决方案

这实际上是我认为应该这样做的,但我得到了一个异常错误:“双向或单向源绑定无法在类型为'WpfApplication5.MainWindow+MyImage'的只读属性'name'上工作。”-如果有帮助,我已经在第一篇文章中包含了我的代码。谢谢。谷歌搜索会让你发现:我想这就是问题所在,因为你的文本框会允许用户更改分配给它的值。你可能想使用某种标签,除非你想让人们改变名字?啊,我明白了。非常感谢。我把它改为:Text=“{Binding SelectedItem.Name,ElementName=listBox1,Mode=OneWay}”,它工作得非常好。谢谢你帮我理解。
ImageItem selectedImage = (ImageItem)listBox1.SelectedItem;