0

我只是想我的一些旧应用程序从 Win-forms 迁移到 WPF。ListBox在我的旧 win-form 应用程序中,我可以使用下面的代码向 a 发送一个数组。

在我的新 WPF 应用程序中,这不会填充ListBox. 奇怪的是它不会出错,它只是不起作用。

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

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;
    }
}
4

1 回答 1

2

在 WPF 中,您应该在后面的代码中填充属性并将您的 UI 元素绑定到这些属性,从后面的代码中引用/访问 UIElements 不是一个好习惯。

这是一个基于我认为您正在尝试做的模型示例。

xml:

<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));
    }
}

结果:

在此处输入图像描述

于 2013-02-03T23:11:35.483 回答