2012-05-31 80 views

回答

2

經過進一步的探索,我提出了一個很好的解決方案。希望有人會發現它很有用:

RadWindow PopupWindow; 
public SampleView() 
{ 
    InitializeComponent(); 
    PopupWindow = new RadWindow { Header = "Details", Name = "radWindow" }; 
    PopupWindow.Loaded += new RoutedEventHandler(PopupWindow_Loaded); 
} 
void PopupWindow_Loaded(object sender, RoutedEventArgs e) 
{ 
    Canvas canvas = Framework.Traversal.FirstVisualAncestorOfType<Canvas>(PopupWindow); 
    canvas.MouseLeftButtonDown -= canvas_MouseLeftButtonDown; 
    canvas.MouseLeftButtonDown += canvas_MouseLeftButtonDown; 
} 
void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    Point mousepoint = e.GetPosition(null); 
    var elements = VisualTreeHelper.FindElementsInHostCoordinates(mousepoint, PopupWindow); 
    if (elements.Count() == 0) 
     PopupWindow.Close(); 
} 
+0

這可能會起作用,但@ sky-dev下面答案中的建議解決方案要簡單得多,而且效果很好。 – sprite

12

這個怎麼樣?

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    protected override void OnDeactivated(EventArgs e) 
    { 
     base.OnDeactivated(e); 
     Close(); 
    } 
} 
+2

這是一個更簡單的解決方案,它的工作原理。您也可以將事件處理程序添加到XAML中的Deactivated事件或代碼中:'Deactivated =「Window_Deactivated」'<! - in XAML - > 或'Deactivated + = Window_Deactivated;'/*分別代碼* /。 然後只需調用close();在處理程序中。 – sprite

相關問題