2014-06-06 15 views
0

我试图学习WPF应用程序。我尝试在WPF中使用ninject进行DI,并且完美地工作。然后,我尝试创建登录表单。在这部分我有一些问题,我无法将数据从视图绑定到虚拟机,当点击按钮。如何绑定数据时按钮点击从视图到wpf的视图模型与Ninject依赖注入

这是我的代码

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; } 
} 

怎么样,解决这个问题?可有一个人帮我

回答

0

只需

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

我已经尝试解决方案之前我张贴到计算器,但仍然没有绑定 –

+0

然后我不知道你的上下文是已知的...你有什么样这在你的输出窗口? '错误:BindingExpression路径错误:在'StackOverflow.MainViewModel'找​​不到'Authorize'属性。 BindingExpression:Path ='Authorize'DataItem ='StackOverflow.MainViewModel';目标元素是'Windows.UI.Xaml.Controls.Button'(Name ='null');目标属性是'Command'(键入'ICommand')' – bdimag

相关问题