2017-08-08 275 views
0

我想在點擊按鈕時顯示一些窗口。窗口應正下方的按鈕被放置(綠圈是按鈕,白色是新窗口):在MVVM中點擊的按鈕的位置打開彈出窗口或窗口

enter image description here

如何獲得按鈕的位置設定在新窗口中的位置MVVM?

我在這樣的視圖中的按鈕:

<Button Command="{Binding LoginCommand}"/> 

在視圖模型的命令:

private void ExecuteLoginCommand() 
{ 
    var dialog = new UserLogin(); 
    dialog.ShowDialog(); 
} 

綁定按鈕VM的位置,然後將其發送到新窗戶可能是一個解決方案,但它是一個很好的?

回答

0

您可以使用TransformToAncestor方法檢索Button在視圖中的相對位置:Get Absolute Position of element within the window in wpf

在調用視圖模型的命令之前,您可以在視圖的代碼隱藏或附加行爲(https://www.codeproject.com/Articles/28959/Introduction-to-Attached-Behaviors-in-WPF)中執行此操作。

一旦你的座標,你可以通過這些作爲參數的命令:

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    Button button = sender as Button; 
    Point relativePoint = button.TransformToAncestor(this).Transform(new Point(0, 0)); 

    var vm = DataContext as YourViewModel; 
    //pass the Point itself or create a custom class yourself and pass an instance of this one here 
    vm.LoginCommand.Execute(relativePoint); 
} 
相關問題