C# 如何将数组发送到列表框?

C# 如何将数组发送到列表框?,c#,wpf,winforms,listbox,folderbrowserdialog,C#,Wpf,Winforms,Listbox,Folderbrowserdialog,我正在尝试将我的一些旧应用程序从Win表单迁移到WPF。在我以前的win form应用程序中,我可以使用下面的代码向列表框发送一个数组 在我的新WPF应用程序中,这不会填充列表框。奇怪的是它没有出错,只是不起作用 …当我第一次测试应用程序时,它确实出错了,我收到一条消息说我没有权限,但现在它只显示消息说“完成”,而不做任何操作 private void btnFolderBrowser_Click(object sender, RoutedEventArgs e) { getFileSt

我正在尝试将我的一些旧应用程序从Win表单迁移到WPF。在我以前的win form应用程序中,我可以使用下面的代码向
列表框发送一个数组

在我的新WPF应用程序中,这不会填充
列表框
。奇怪的是它没有出错,只是不起作用

…当我第一次测试应用程序时,它确实出错了,我收到一条消息说我没有权限,但现在它只显示消息说“完成”,而不做任何操作

private void btnFolderBrowser_Click(object sender, RoutedEventArgs e)
{
    getFileStructure();
    System.Windows.Forms.MessageBox.Show("done!");
}

private void getFileStructure()
{
    string myDir = "";
    int i;
    string filter = txtFilter.Text;

    FolderBrowserDialog fbd = new FolderBrowserDialog();
    fbd.RootFolder = Environment.SpecialFolder.Desktop;
    fbd.ShowNewFolderButton = false;
    fbd.Description = "Browse to the root directory where the files are stored.";

    if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        myDir = fbd.SelectedPath;

    try
    {
        txtRootDir.Text = fbd.SelectedPath;
        string[] fileName = Directory.GetFiles(myDir, filter, SearchOption.AllDirectories);

        for (i = 0; i < fileName.Length; i++)
            lstFileNames.Items.Add(fileName[i]);
    }

    catch (System.Exception excep)
    {
        System.Windows.Forms.MessageBox.Show(excep.Message);
        return;
    }
}
private void btnFolderBrowser\u单击(对象发送方,路由目标)
{
getFileStructure();
System.Windows.Forms.MessageBox.Show(“完成”);
}
私有void getFileStructure()
{
字符串myDir=“”;
int i;
字符串过滤器=txtFilter.Text;
FolderBrowserDialog fbd=新建FolderBrowserDialog();
fbd.RootFolder=Environment.SpecialFolder.Desktop;
fbd.ShowNewFolderButton=false;
fbd.Description=“浏览到存储文件的根目录。”;
if(fbd.ShowDialog()==System.Windows.Forms.DialogResult.OK)
myDir=fbd.SelectedPath;
尝试
{
txtRootDir.Text=fbd.SelectedPath;
string[]fileName=Directory.GetFiles(myDir、filter、SearchOption.AllDirectories);
对于(i=0;i
在WPF中,您应该在代码隐藏中填充属性并将UI元素绑定到这些属性,从代码隐藏中引用/访问UIElements不是一个好的做法

下面是一个基于我认为您正在尝试的内容的模型示例

Xaml:


代码:

public分部类主窗口:窗口,INotifyPropertyChanged
{
私有字符串_textRootDir;
私有字符串_textFilter=string.Empty;
私有字符串\u选择的文件;
私有ObservableCollection_myFiles=新ObservableCollection();
公共主窗口()
{
初始化组件();
}
公共可观察收集MyFiles
{
获取{return\u myFiles;}
设置{u myFiles=value;}
}
公共字符串选择文件
{
获取{return\u selectedFile;}
设置{u selectedFile=value;NotifyPropertyChanged(“selectedFile”);}
}
公共字符串文本过滤器
{
获取{return\u textFilter;}
设置{u textFilter=value;NotifyPropertyChanged(“textFilter”);}
}
公共字符串TextRootDir
{
获取{return\u textRootDir;}
设置{u textRootDir=value;NotifyPropertyChanged(“textRootDir”);}
}
私有无效BTN文件夹浏览器\单击(对象发送者,路由目标e)
{
GetFileStructure();
MessageBox.Show(“完成!”);
}
私有void GetFileStructure()
{
字符串myDir=“”;
System.Windows.Forms.FolderBrowserDialog fbd=新建System.Windows.Forms.FolderBrowserDialog();
fbd.RootFolder=Environment.SpecialFolder.Desktop;
fbd.ShowNewFolderButton=false;
fbd.Description=“浏览到存储文件的根目录。”;
if(fbd.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
myDir=fbd.SelectedPath;
尝试
{
TextRootDir=fbd.SelectedPath;
MyFiles.Clear();
foreach(Directory.GetFiles(myDir、TextFilter、SearchOption.AllDirectories)中的var文件)
{
添加(文件);
}
}
捕获(例外例外)
{
MessageBox.Show(消息除外);
返回;
}
}
}
公共事件属性更改事件处理程序属性更改;
public void NotifyPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
结果:


您是否尝试放置断点以确保列表确实已填充?文件名中是否有任何内容?ps名称不正确就是这样…名称不正确是什么意思?对不起,如果这是一个愚蠢的问题,我不是一个开发人员,但我想知道适当的标准。谢谢。令人尴尬的是,我不得不承认,我复制粘贴并使其生效所花的时间比你从头开始所花的时间要长。现在我必须去学习可观察的集合。
<Window x:Class="WpfApplication14.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" Name="UI">
    <StackPanel DataContext="{Binding ElementName=UI}">
        <TextBox Text="{Binding TextRootDir}" IsReadOnly="True" />
        <ListBox ItemsSource="{Binding MyFiles}" SelectedItem="{Binding SelectedFile}" Height="244" />
        <TextBox Text="{Binding TextFilter, UpdateSourceTrigger=PropertyChanged}" />
        <Button Content="Browse" Click="btnFolderBrowser_Click" >
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="IsEnabled" Value="True" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding TextFilter}" Value="">
                            <Setter Property="IsEnabled" Value="False" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>
    </StackPanel>

</Window>
public partial class MainWindow : Window, INotifyPropertyChanged
{
    private string _textRootDir;
    private string _textFilter = string.Empty;
    private string _selectedFile;
    private ObservableCollection<string> _myFiles = new ObservableCollection<string>();

    public MainWindow()
    {
        InitializeComponent();
    }

    public ObservableCollection<string> MyFiles
    {
        get { return _myFiles; }
        set { _myFiles = value; }
    }

    public string SelectedFile
    {
        get { return _selectedFile; }
        set { _selectedFile = value; NotifyPropertyChanged("SelectedFile"); }
    }

    public string TextFilter
    {
        get { return _textFilter; }
        set { _textFilter = value; NotifyPropertyChanged("TextFilter"); }
    }

    public string TextRootDir
    {
        get { return _textRootDir; }
        set { _textRootDir = value; NotifyPropertyChanged("TextRootDir"); }
    }

    private void btnFolderBrowser_Click(object sender, RoutedEventArgs e)
    {
        GetFileStructure();
        MessageBox.Show("done!");
    }

    private void GetFileStructure()
    {
        string myDir = "";
        System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
        fbd.RootFolder = Environment.SpecialFolder.Desktop;
        fbd.ShowNewFolderButton = false;
        fbd.Description = "Browse to the root directory where the files are stored.";

        if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            myDir = fbd.SelectedPath;
            try
            {
                TextRootDir = fbd.SelectedPath;
                MyFiles.Clear();
                foreach (var file in Directory.GetFiles(myDir, TextFilter, SearchOption.AllDirectories))
                {
                    MyFiles.Add(file);
                }
            }
            catch (Exception excep)
            {
                MessageBox.Show(excep.Message);
                return;
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}