2013-02-25 2 views
0

마우스를 올리면 팝업 메뉴가있는 WPF 응용 프로그램이 있습니다. 팝업 창에는 열 수있는 다른 파일 목록이 있습니다 (예 : pdf, Excel 등)파일을 열 때 WPF 목록 선택 문제

두 번 클릭하여 파일을 탐색하고 선택할 수 있습니다. 원하는대로 열 수 있습니다.

하지만 지금은 다른 파일로 이동 한 경우 지금 다른 파일을 선택하면 내가 가져가 선택에 지금

, 작동하지 않는 것을 알 수 있습니다, 원본 파일이 다시 열립니다.

나는 Process.Start를 사용하여 파일의 전체 경로를 메서드에 전달합니다.

응용 프로그램은 그래서 여기에 나는이 더

으로보고 작성한 테스트 응용 프로그램에 대한 몇 가지 발췌은 공정한 크기의 메인 창에 대한 XAML

<Window x:Class="TestPopupIssue.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"> 

    <Canvas Margin="5" Background="Red" Width="200" Height="150" > 

     <Rectangle Name="rectangle1" 
      Canvas.Top="60" Canvas.Left="50" 
      Height="85" Width="60" 
      Fill="Black" MouseEnter="rectangle1_MouseEnter" MouseLeave="rectangle1_MouseLeave" /> 

     <Popup x:Name="PopupWindow" PlacementTarget="{Binding ElementName=rectangle1}" Placement="Top" MouseEnter="rectangle1_MouseEnter" MouseLeave="rectangle1_MouseLeave"> 
      <ListBox MinHeight="50" ItemsSource="{Binding Files}" MouseDoubleClick="FileList_MouseDoubleClick"`enter code here` x:Name="FileList" /> 
     </Popup> 
    </Canvas> 


</Window> 

MainWindow.xaml.cs를

public partial class MainWindow : Window 
    { 
     FileList f; 

     public MainWindow() 
     { 
      InitializeComponent(); 

      f = new FileList(); 
      f.PopulateFiles(); 

      this.DataContext = f; 
     } 

     private void FileList_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
     { 
      if (FileList.SelectedItem != null) 
      { 
       string item = FileList.SelectedItem as string; 

       if (item != null) 
       { 
        System.Diagnostics.Process.Start(item);    
       } 

      } 
     } 

     private void rectangle1_MouseEnter(object sender, MouseEventArgs e) 
     { 
      PopupWindow.IsOpen = true; 
     } 

     private void rectangle1_MouseLeave(object sender, MouseEventArgs e) 
     { 
      PopupWindow.IsOpen = false; 
     } 
    } 

그리고 단지 파일

01라는 파일 경로의 일반적인 문자열 목록이 파일 목록 클래스가있다

감사합니다.

+0

두 컨트롤에서 동일한 MouseEnter와 MouseLeave를 실행하는 이유는 무엇입니까? 어떻게 든 단순화해야한다고 생각합니다. – Paparazzi

+0

이 샘플은 단순하기 때문에 원본에는 페이드 등이 있습니다. 여기에서 사각형을 떠나면 다른 마우스 센터없이 마우스를 가져 가면 팝업이 닫힙니다. – user2107006

답변

1

파일을 여는 프로세스가 시작되면 Sample-Application을 테스트했습니다. 파일을 여는 응용 프로그램에서 포커스를 도둑 맞으십시오. 어떻게 든 팝업의 ListBox는 Window가 포커스를 잃었을 때 SelectedItem을 변경할 수 없습니다.

불행하게도 나는 창에서 포커스를 다시 얻을 수 없었지만, this.SetFocus()는 나를 위해 일하지 않았다.

어쨌든 다른 가능한 해결책은 파일을 열 때 팝업을 닫는 것입니다.

private void FileList_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
{ 
    if (FileList.SelectedItem != null) 
    { 
     string item = FileList.SelectedItem as string; 

     if (item != null) 
     { 
      System.Diagnostics.Process.Start(item); 
      PopupWindow.IsOpen = false; 
     } 
    }  
} 

이렇게하면 ListBox에서 selectedItem을 다시 업데이트 할 수 있습니다.

희망이 도움이됩니다.

+0

감사합니다. – user2107006