如何使用Ninject Dependency Injection在按钮单击视图到wpf中的viewmodel时绑定数据

时间:2014-06-06 19:48:05

标签: wpf mvvm mvvm-light

我试着学习wpf应用程序。我尝试在WPF中使用ninject进行DI,并且工作得很好。然后,我尝试创建登录表单。在这部分我有一些问题,当点击按钮时,我无法将数据从视图绑定到VM。

这是我的代码

LoginUserControl

<UserControl x:Class="Middleware_v2._0_with_Modern_Ui.UserControls.LoginUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
         xmlns:vm="clr-namespace:Middleware_v2._0_with_Modern_Ui.ViewModel"
         xmlns:Custom="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
         xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
         mc:Ignorable="d" DataContext="{Binding Login, Source={StaticResource Locator}}">

<Border Width="400" Height="300" BorderBrush="LightBlue" CornerRadius="5" Background="SkyBlue" Margin="0,-100,0,0">
    <StackPanel Margin="0,0,10,0" Width="400">
        <Label VerticalAlignment="Center" HorizontalAlignment="Center" 
                       Content="Login Form" Margin="0,30,0,0" FontSize="20" FontWeight="Bold"/>
        <Label Content="User Name" Margin="84,45,197,0"/>
        <TextBox Text="{Binding UserName, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="150" Height="25" Margin="165,-20,85,0"/>
        <Label Content="Password" Margin="84,15,197,0"/>
        <PasswordBox x:Name="txt_password" Width="150" Height="25" Margin="165,-20,85,0"/>
        <Button Width="100" Height="30" Margin="-110,20,0,0" Content="Login" />
        <Custom:Interaction.Triggers>
            <Custom:EventTrigger EventName="Click">
                <GalaSoft_MvvmLight_Command:EventToCommand x:Name="btnClicked" Command="{Binding Authorize, Mode=OneTime}"/>
            </Custom:EventTrigger>
        </Custom:Interaction.Triggers>
        <!--<Button Width="100" Height="30" Margin="110,-30,0,200" Content="Exit" />-->
    </StackPanel>
    <Border.Effect>
        <DropShadowEffect ShadowDepth="0" BlurRadius="15" Color="Gray" Direction="10"/>
    </Border.Effect>
</Border>

LoginViewModel

public class LoginViewModel : ViewModelBase, ILoginViewModel
{
    private readonly IAccountService _accountService;

    public LoginViewModel(IAccountService _accountService)
    {
        this._accountService = _accountService;
        Authorize = new RelayCommand(() => CheckAuthorized(), () => true);
    }

    public RelayCommand Authorize { get; set; } 

    private void CheckAuthorized()
    {
        User newUser = new User();
        newUser.LoginName = _username;
        newUser.LoginPassword = _password;

        User user = _accountService.AuthenticationUser(newUser, 1);
        throw new System.NotImplementedException();
    }

}

LoginViewModel接口

public interface ILoginViewModel
{
    RelayCommand Authorize { get; set; } 
}

怎么解决这个问题?可以有人帮助我

1 个答案:

答案 0 :(得分:0)

简单地

<Button Width="100" Height="30" Margin="-110,20,0,0" Content="Login" Command="{Binding Authorize}" />