【问题标题】:Get name of selected listbox item (WPF C#)?获取所选列表框项目的名称(WPF C#)?
【发布时间】:2011-08-10 08:41:28
【问题描述】:

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

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

任何人都可以为我指出正确的方向吗?谢谢。

如果有帮助,这是我的 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;
        }
    }        

}

【问题讨论】:

    标签: wpf listbox selecteditem


    【解决方案1】:

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

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

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

      <ListBox .... SelectedItem="{Binding SelectedImage}">
      
    • 直接从代码隐藏访问SelectedItem 属性:

      ImageItem selectedImage = (ImageItem)listBox1.SelectedItem;
      

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

    【讨论】:

      【解决方案2】:

      看看这个问题。

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

      在你的情况下使用

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

      【讨论】:

      • 这实际上是我认为应该完成的方式,但我收到异常错误:“TwoWay 或 OneWayToSource 绑定无法对“WpfApplication5”类型的只读属性“名称”起作用.MainWindow+MyImage'。” - 如果有帮助,我已将我的代码包含在第一篇文章中。谢谢。
      • 谷歌搜索会引导你到这个:stackoverflow.com/questions/590269/… 我希望这是问题,因为你的文本框将允许用户更改分配给它的值。除非您希望人们能够更改名称,否则您可能还是想使用某种标签?
      • 啊,我明白了。非常感谢。我将其更改为:Text="{Binding SelectedItem.Name, ElementName=listBox1, Mode=OneWay}",并且效果很好。感谢您帮助我理解。
      猜你喜欢
      • 2017-06-09
      • 2013-05-17
      • 2011-06-08
      • 1970-01-01
      • 2011-09-25
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多