2010-06-01 2 views
3

Window1.xaml 메인 창이 있습니다. 어떤 이벤트가 발생하면 UserControl EditFile.xaml을 표시합니다.현재 UserControl 닫기

코드 뒤에 다음과 같습니다

public static int whichSelected = -1; 
private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    //searchEditPanel.Children.Clear(); 
    whichSelected = listViewFiles.SelectedIndex; 
    searchEditPanel.Children.Add(_EditFileControle);  //this is Grid 
} 

그리고 지금은, 내가 어떻게 열/a를 그런 식으로 버튼을 누르거나 무엇인가를 취소를 클릭하여 컨텐츠에서 UserControl을 추가 닫을 수 있습니다?

답변

1

시도해 보셨습니까?

searchEditPanel.Children.Remove(_EditFileControle); 

또 다른 제안 :

어쩌면이 도움이 : http://sachabarber.net/?p=162

그것은하지 않는 경우 귀하의 UserControl에 속성을 추가

public UserControl ParentControl {get;set;} 

를 이제 코드를 수정 :

private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    //searchEditPanel.Children.Clear(); 
    whichSelected = listViewFiles.SelectedIndex; 
    _EditFileControle.ParentControl = this; 
    searchEditPanel.Children.Add(_EditFileControle);  //this is Grid 
} 
이제

이 할 수 있어야 : 시도하여 버튼 클릭 핸들러에서

// Somewhere in your UserControl 
if (this.ParentControl != null) 
    this.ParentControl.Children.Remove(this); 
+1

예 - 작동하지만 부모 (Window1)에서 실행 중입니다. 내 자녀 (EditFile.xaml.cs 파일)에서 UserControl을 닫으려면 Cancel 버튼을 클릭해야합니다. – BlueMan

1

가시성 "닫으려는 컨트롤"속성을 접음으로 설정할 수 있습니다.

이렇게하면 더 이상 표시되지 않지만 나중에 다시 사용해야하는 경우 시각적 트리에 계속 표시됩니다.

2

을 :

Window parentWindow = (Window)this.Parent; 
parentWindow.Close(); 
+0

개체 참조가 개체의 인스턴스로 설정되지 않았습니다. – albatross

10
Window.GetWindow(this).Close(); 

당신은 새로운 변수를 사용할 필요가 없습니다, 당신이 그것을 사용할 수 있습니다 직접.

+0

그냥 궁금해 ...이 때문에 'UserControl.Unloaded' 이벤트가 발생합니까? – IAbstract