[英]WPF Dynamic Listbox with different output
首先,我想說的是,我在互聯網上進行搜索時沒有發現任何東西。 我想創建一個動態的歌曲列表,當用戶添加一首歌曲時,將添加帶有名稱和長度的新對象,並且當他選擇該歌曲時,他將獲得所選歌曲的路徑。 這是更好理解的代碼:
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進行制作,或者我需要制作將保存所有路徑的數組?
首先,您可能需要看一下MVVM編程。 這個概念不是那么容易理解,甚至很難應用,但是一旦掌握了它,您將能夠立即創建良好的UI,並且對於您要嘗試的操作而言,它尤其完美。
現在解決您的問題。 FileInfo.Name僅存儲文件名而不存儲路徑,因此,如果您不單獨存儲路徑,則將無法僅從文件名取回路徑。
KillaKem的解決方案看起來不錯,但是我不建議為此使用Tag屬性。 只需創建一個Collections :: Generic :: List(請不要使用數組)類成員來存儲路徑,然后使用selectedIndexChange事件更新PathSearchBox。
使用MVVM,您可以將ListBox鏈接到存儲路徑和文件名的Dictionary,並僅顯示Filename,可以隨時自行研究MVVM。
問候,Xaser
我想您應該為您的歌曲使用,因為您正在編寫WPF應用程序,即您的歌曲對象的ObservableCollection ,以便使用更新的對象來創建適當的ListBox。
然后,您必須創建一個具有其自身屬性的正確Song
類,例如SongName
或SongPath
。 在此類中,您將實現INotifyPropertyChanged接口,並且對於每個屬性,您將引發一個適當的OnPropertyChanged
事件。
使用此實現,一旦加載一首歌曲並將其添加到歌曲列表,您的ListBox將相應地顯示更新后的集合。
有關ObservableCollection的更多信息,請參見此處 。
如果您想看一下代碼示例,請在此答案中寫一個開發ObservableCollection的示例。
您可以嘗試一些類似的方法,
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 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;
}
}
}
尚未測試代碼,但它應該可以工作或接近工作。
這些是您可以使用的列表框中的SelectionChanged事件。 但我建議使用bi
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.