【发布时间】:2019-11-12 19:38:40
【问题描述】:
我无法将数组绑定到 ListBox。
我在网上看了很多方法,但都没有成功。似乎数据显示在 ItemsSource 中,但未显示在列表框中。
代码:
public partial class MainWindow : ThemedWindow
{
public string selectedItem { get; set; }
public ObservableCollection<string> MyFiles { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private void Button_Click(object sender,RoutedEventArgs e)
{
string[] filePaths = Directory.GetFiles(TextEdit.Text);
MyFiles = new ObservableCollection<string>(filePaths);
selectedItem = MyFiles[0];
foreach (string filePath in filePaths)
{
Console.WriteLine(filePath);
string file = Path.GetFileName(filePath);
Console.WriteLine(file);
}
}
}
XAML:
<ListBox x:Name="listBox"
BorderThickness="2"
Height="352"
HorizontalAlignment="Center"
ItemsSource="{Binding MyFiles}"
Margin="92,0,95.6,10"
SelectedItem="{Binding selectedItem, Mode=TwoWay}"
SelectionMode="Single"
VerticalAlignment="Bottom"
Width="606"
/>
没有错误消息只是不在列表框中显示输出
【问题讨论】:
-
感谢您的意见。但是,我是一个业余爱好者,并没有理解它。
标签: c# data-binding listbox