MVVM:关闭auth窗口并打开另一个窗口

时间:2015-05-25 19:15:40

标签: c# wpf mvvm

人。如果我想要auth用户,关闭身份验证窗口并打开包含一些信息的新窗口,我需要做什么?

<Window x:Class="VSgonnadie.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:nspase="clr-namespace:VSgonnadie"
    Title="Auth" Height="236.564" Width="166.922">
<Window.DataContext>
    <nspase:ViewModel />
</Window.DataContext>
<Grid Margin="0,0,2,1">
    <Button x:Name="AuthButton" Content="Авторизация" HorizontalAlignment="Left" Margin="19,152,0,0" Command="{Binding Log}" CommandParameter="{Binding ElementName=MainWindow}" VerticalAlignment="Top" Width="120" Height="34" Grid.ColumnSpan="2"/>
    <TextBox x:Name="Login" HorizontalAlignment="Left" Height="23" Margin="19,63,0,0" TextWrapping="Wrap" Text="{Binding Login}" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2"/>
    <TextBox x:Name="Password" HorizontalAlignment="Left" Height="23" Margin="19,111,0,0" TextWrapping="Wrap" Text="{Binding Password}" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2"/>
    <Label Content="Please, log- in" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="4.176,4.97" Margin="19,19,0,0" Width="120" Grid.ColumnSpan="2"/>

</Grid>

我试图通过将窗口名称传递给RelayCommand来做到这一点,但是它没有工作

public class ViewModel: ViewModelBase
{
    public string Login {
        get;
        set;
    }
    public string Password { 
        get; 
        set;
    }
    public ViewModel(){
        Login = "Login";
        Password = "Password";
    }

    private ICommand login;
    public ICommand Log
    {
        get{
            //here i'm thinking i'll get current window in 'win' param
            return login ?? (login = new RelayCommand<Window>((win)=>{
            }));

        }
    }
}    

1 个答案:

答案 0 :(得分:1)

您将CommandParameter绑定到ElementName=MainWindow。您在范围内没有具有该名称的元素(至少从您显示的内容)。您应该看到输出窗口中记录的绑定错误。

x:Name="MainWindow"添加到您的Window声明中。

<Window x:Class="VSgonnadie.MainWindow" x:Name="MainWindow" ...