起初我想说我在互联网上搜索并没有找到任何东西。我想制作一个动态歌曲列表,当用户添加歌曲时,将添加带有名称和长度的新对象,当他选择这首歌时,他将获得所选歌曲的路径。这是更好理解的代码:
XAML:
<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 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>
和代码:
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);
            }
        }
    }
}
所以“路径”文本框在哪里,我想获取当前选定的歌曲路径。是否可以使用 ItemBox 制作或者我需要制作可以保存所有路径的数组?