【问题标题】:MVVM light command not firingMVVM 灯光命令未触发
【发布时间】:2014-08-03 20:06:42
【问题描述】:

我有一个简单的登录表单,我正在尝试使用它,但由于某种原因,登录按钮没有调用 LoginCommand,我不知道为什么,有人可以帮我...

这是表单的代码:

<Window x:Class="App.Views.Login"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Login" Height="200" Width="340"
    DataContext="{Binding Login, Source={StaticResource Locator}}" ResizeMode="NoResize">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="10"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="10"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Label Content="Username:" HorizontalAlignment="Right" VerticalAlignment="Top" Grid.Row="1" Grid.Column="1" Padding="0,1,5,0"/>
    <Label Content="Password:" HorizontalAlignment="Right" VerticalAlignment="Top" Padding="0,1,5,0" Grid.Row="3" Grid.Column="1"/>
    <TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" Height="20" Grid.Row="1" Grid.Column="2" Text="{Binding UserName}"/>
    <PasswordBox x:Name="password" HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" Height="20" Grid.Row="3" Grid.Column="2"/>
    <Button Command="{Binding Source=LoginCommand}" CommandParameter="{Binding ElementName=password}" Content="Login" Grid.Row="5" Grid.ColumnSpan="2" Grid.Column="1" HorizontalAlignment="Center" Width="60"/>
</Grid>
</Window>

这是我的 LoginViewModel:

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Net.Http;
using System.Windows.Controls;
using System.Windows.Input;

namespace App.ViewModels
{
public class LoginViewModel : ViewModelBase
{
    public Login LoginModel;

    public ICommand LoginCommand;

    public ICommand RegisterViewCommand;

    public ICommand ExitViewCommand;

    public LoginViewModel()
    {
        LoginModel = new Models.Login();
        LoginCommand = new RelayCommand<object>(p => Login(p));
        RegisterViewCommand = new RelayCommand(() => Register());
        ExitViewCommand = ApplicationCommands.Close;
    }

    public string UserName
    {
        get
        {
            return LoginModel.UserName;
        }
        set
        {
            if(value != LoginModel.UserName)
            {
                LoginModel.UserName = value;
                RaisePropertyChanged("UserName");
            }
        }
    }

    #region Private Helpers

    private void Register()
    {
        throw new NotImplementedException();
    }

    private async void Login(object parameter)
    {
        var passwordBox = parameter as PasswordBox;
        LoginModel.Password = passwordBox.Password;
        LoginModel.RememberMe = true;
        var response = await HttpClientSingleton.Instance.PostAsJsonAsync("http://localhost:52841/Account/ClientLogin", LoginModel);
        bool success;
        if (bool.TryParse(await response.Content.ReadAsStringAsync(), out success))
        {
        }
    }

    #endregion Private Helpers
}
}

我在 Login 方法的第一行设置了一个断点,但它从未真正命中它。我还更改了 UserName 属性以返回一个预定义的字符串来测试 DataContext 绑定,并且 UserName 文本框显示这个预定义的字符串,所以我很确定表单被正确绑定。有什么建议吗?

【问题讨论】:

    标签: wpf mvvm-light


    【解决方案1】:

    我认为您的问题是您绑定到公共字段而不是属性。

    您可以看到可以用作绑定源的东西here on MSDN。字段不是其中之一。

    因此,将命令作为属性公开应该可以:

    public ICommand LoginCommand { get; private set; }
    

    【讨论】:

    • 这是一个很好的建议,所以我要为你 +1,但它不起作用。
    • 但是,在从 Command 绑定中删除 Source=LoginCommand 后,仅使用 {Binding LoginCommand} 它确实可以使用属性,所以谢谢,我会接受这个答案。
    • 对,因为{Binding KEY}和{Binding Path=KEY}一样,不是但不是Source
    猜你喜欢
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多