2013-02-03 28 views
0

我只是想遷移一些我從贏-形式WPF老的應用程序。在我以前的雙贏形式的應用程序中,我可以使用下面的代碼將數組發送到ListBox如何將數組發送到列表框?

在我的新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; 
    } 
} 
+0

你嘗試把一個斷點,以確保實際上正在填充列表? – Blachshma

+0

那麼有沒有什麼文件名?,PS沒有很好的命名,這是它... –

+0

你是什麼意思,沒有很好的名字?對不起,如果這是一個愚蠢的問題,我不是一個開發人員,但我想知道適當的標準。 – bolilloBorracho

回答

2

在WPF你應該填充後面的代碼屬性和綁定你的UI元素的屬性,它不是好的做法從代碼中引用/訪問UI元素的後面。

這是基於什麼,我認爲你正在嘗試做一個樣機的例子。

的XAML:

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

結果:

enter image description here

+0

謝謝。我不得不承認的尷尬之處是,複製粘貼並使其發揮作用需要花費更長的時間,而不是從頭開始。現在我必須去了解ObservableCollections。 – bolilloBorracho

相關問題