C# 具有不同输出的WPF动态列表框

C# 具有不同输出的WPF动态列表框,c#,wpf,C#,Wpf,起初我想说我在网上搜索了一下,什么也没找到。我想制作一个动态歌曲列表,当用户添加歌曲时,新对象将添加名称和长度,当他选择这首歌曲时,他将获得所选歌曲的路径。下面是要更好理解的代码: XAML: 和代码: using Microsoft.Win32; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Thread

起初我想说我在网上搜索了一下,什么也没找到。我想制作一个动态歌曲列表,当用户添加歌曲时,新对象将添加名称和长度,当他选择这首歌曲时,他将获得所选歌曲的路径。下面是要更好理解的代码:

XAML:


和代码:

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ListBox
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openfile = new OpenFileDialog();
            openfile.DefaultExt = ".mp3";
            openfile.Filter = "mp3 | *.mp3";
            Nullable<bool> result = openfile.ShowDialog();
            if (result == true)
            {
                String file = openfile.FileName;
                FileInfo fileinfo = new FileInfo(file);

                SongList.Items.Add(fileinfo.Name);
            }
        }
    }
}
使用Microsoft.Win32;
使用制度;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
名称空间列表框
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
OpenFileDialog openfile=新建OpenFileDialog();
openfile.DefaultExt=“.mp3”;
openfile.Filter=“mp3 |*.mp3”;
Nullable result=openfile.ShowDialog();
如果(结果==真)
{
String file=openfile.FileName;
FileInfo FileInfo=新的FileInfo(文件);
SongList.Items.Add(fileinfo.Name);
}
}
}
}

所以,在“路径”文本框中,我想获得当前选定的歌曲路径。是否可以使用ItemBox制作,或者我需要制作一个数组来保存所有路径?

这是您可以使用的列表框中的SelectionChanged事件。但我建议您使用bi,您可以尝试类似的方法

XAML:


返回代码:

namespace ListBox
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openfile = new OpenFileDialog();
            openfile.DefaultExt = ".mp3";
            openfile.Filter = "mp3 | *.mp3";
            Nullable<bool> result = openfile.ShowDialog();
            if (result == true)
            {
                String file = openfile.FileName;
                FileInfo fileinfo = new FileInfo(file);

                SongList.Items.Add(fileinfo.Name);

                var pathList = SongList.Tag as List<string>;
                pathList.Add(fileinfo.DirectoryName);
                SongList.Tag = pathList;
            }
        }

         private void Selection_Changed(object sender, EventArgs e)
        {
              var myListBox = sender as ListBox;
              var myPathList = myListBox.Tag as List<string>;
              var filePath = myPathList[myListBox.SelectedIndex];

              pathLabel.Content = filePath;
        }
    }
}
名称空间列表框
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
OpenFileDialog openfile=新建OpenFileDialog();
openfile.DefaultExt=“.mp3”;
openfile.Filter=“mp3 |*.mp3”;
Nullable result=openfile.ShowDialog();
如果(结果==真)
{
String file=openfile.FileName;
FileInfo FileInfo=新的FileInfo(文件);
SongList.Items.Add(fileinfo.Name);
var pathList=SongList.tagas List;
添加(fileinfo.DirectoryName);
Tag=pathList;
}
}
私有无效选择\u已更改(对象发送方、事件参数e)
{
var myListBox=发送方作为列表框;
var myPathList=myListBox.tagas List;
var filePath=myPathList[myListBox.SelectedIndex];
pathLabel.Content=filePath;
}
}
}

尚未测试代码,但它应该正在工作或即将工作。

首先,您可能想了解一下MVVM编程。这个概念不是那么容易理解,甚至更难应用,但一旦你掌握了它,你将能够在短时间内创建好的用户界面,它对于你正在尝试做的事情来说尤其完美

现在谈谈你的问题。FileInfo.Name只存储文件名而不是路径,因此如果不单独存储路径,则无法仅从文件名获取路径

KillaKem的解决方案看起来相当不错,但是我不建议为此使用Tag属性。只需创建一个Collections::Generic::List(请不要使用数组)类成员来存储路径,然后使用selectedIndexChange事件来更新PathSearchBox

使用MVVM,您可以将列表框链接到一个字典,该字典同时存储路径和文件名,并且只显示文件名,您可以自己研究一下MVVM

问候,,
Xaser

我认为你应该为你的歌曲使用,因为你正在编写一个WPF应用程序,一个你的歌曲对象,以便有一个合适的列表框和你更新的对象

然后,您必须使用自己的属性创建一个适当的
Song
类,如
SongName
SongPath
。在这个类中,您将实现接口 并且,对于每个属性,您将引发相应的
OnPropertyChanged
事件

使用此实现,一旦加载歌曲并将其添加到歌曲列表中,列表框将相应地显示更新的收藏

有关ObservableCollection的更多信息


如果您想查看代码示例,我编写了一个开发ObservableCollection的示例。

是的,我知道,但我可以将歌曲路径存储在列表框中,还是必须为此创建一个数组?类似的东西。问题是ListBox显示的是路径,而不仅仅是歌曲名称。将“文件”添加到ListBox会显示路径,而将“fileinfo.name”添加到ListBox只会显示名称,因此请确保将“fileinfo.name”添加到ListBox。ListBox将显示歌曲的完整路径或仅显示歌曲名称?您面临什么问题?这些项目没有显示在列表框中吗?谢谢。现在我知道我应该学什么来做我需要的一切。现在我要去学习新东西:DGreat!如果您需要一些东西来开始学习MVVM,我可以强烈推荐这本指南,它有点长,但它完美地说明了MVVM如此优秀的原因:尤其是演示项目的编程非常好。另外,如果您没有其他问题,请将其中一个答案标记为您问题的答案。
<Window x:Class="ListBox.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ListBox x:Name="SongList" HorizontalAlignment="Left" Height="278" Margin="10,10,0,0" VerticalAlignment="Top" Width="248"/>
    <TextBlock HorizontalAlignment="Left" Margin="287,124,0,0" TextWrapping="Wrap" Text="Path" VerticalAlignment="Top" Width="194" Height="22"/>
    <Label Name ="pathLabel" Content="Song Path" HorizontalAlignment="Left" Margin="287,98,0,0" VerticalAlignment="Top" Width="194"/>
    <Button Content="Add Song" HorizontalAlignment="Left" Margin="10,293,0,0" VerticalAlignment="Top" Width="132" Click="Button_Click"/>

</Grid>
namespace ListBox
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openfile = new OpenFileDialog();
            openfile.DefaultExt = ".mp3";
            openfile.Filter = "mp3 | *.mp3";
            Nullable<bool> result = openfile.ShowDialog();
            if (result == true)
            {
                String file = openfile.FileName;
                FileInfo fileinfo = new FileInfo(file);

                SongList.Items.Add(fileinfo.Name);

                var pathList = SongList.Tag as List<string>;
                pathList.Add(fileinfo.DirectoryName);
                SongList.Tag = pathList;
            }
        }

         private void Selection_Changed(object sender, EventArgs e)
        {
              var myListBox = sender as ListBox;
              var myPathList = myListBox.Tag as List<string>;
              var filePath = myPathList[myListBox.SelectedIndex];

              pathLabel.Content = filePath;
        }
    }
}