2013-02-03 2 views
0

나는 단지 을 옮기기 위해 Win-forms에서 내 이전 응용 프로그램 중 일부를 WPF로 마이그레이션하려고합니다. 내 오래된 승 - 양식 응용 프로그램에서 나는 ListBox 코드를 아래에 배열을 보낼 수 있습니다.배열을 목록 상자로 보내려면 어떻게해야합니까?

내 새 WPF 응용 프로그램에서는 ListBox이 채워지지 않습니다. 이상한 것은 오류가 발생하지 않는다는 것입니다. 단지 작동하지 않습니다.

... 내가 처음에 응용 프로그램을 테스트했을 때 오류가 있었는데 사용 권한이 없다는 메시지가 표시되었지만 지금은 아무 것도하지 않고 "완료"라는 메시지 만 표시합니다. 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; 
    } 
} 
+0

이 목록이 실제로 채워되고 있는지 확인하기 위해 중단 점을 넣어 시도 했습니까? – Blachshma

+0

그래서 fileName에는 무엇이 있습니까?, ps라는 이름이 잘 붙지 않았습니다 ... –

+0

잘 지명되지 않았다는 것을 무엇을 의미합니까? 미안 해요, 그게 바보 같은 질문이라면, 저는 개발자가 아니지만 적절한 기준을 알고 싶습니다. – bolilloBorracho

답변

2

당신은 뒤에 코드에서 속성을 채우고 이러한 속성에 당신 UI 요소를 결합해야한다,이 아니 좋은 연습 뒤에 코드에서/액세스 UiElements의를 참조하십시오.

다음은 내가 생각하는 바를 기반으로 한 실물 크기 모형입니다.

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