私は私のUIで....プリズムからなぜ私のコマンドはボタンを有効にできませんでしたか?
をDelgateCommandを使用することを学んで、私は私のユーザー名テキストボックスとPasswordBoxを持っています。そして、
<Button Name="button1" Command="{Binding LoginCommand, Mode=TwoWay}" CommandTarget="{Binding ElementName=_UserNameTextBox, Path=Text}">Login</Button>
:
<TextBox Name="_UserNameTextBox" Text="{Binding UserName, Mode=TwoWay}" />
<PasswordBox Name="_PasswordBox"></PasswordBox>
そして、私のログインボタン私のViewModel私は持っています:
string _UserName = string.Empty;
public string UserName
{
get
{
return _UserName;
}
set
{
if (value != _UserName)
{
_UserName = value;
RaisePropertyChanged("UserName");
}
}
}
//For reference the password
PasswordBox _PasswordBox { get; set; }
public DelegateCommand<string> LoginCommand { get; set; }
public LoginViewModel(PasswordBox passwordBox)
{
_PasswordBox = passwordBox;
LoginCommand = new DelegateCommand<string>(
(
//Execute
(str) =>
{
Login(_PasswordBox.Password);
}
),
//CanExecute Delgate
(usr) =>
{
if (string.IsNullOrEmpty(usr) || string.IsNullOrEmpty(_PasswordBox.Password))
return false;
return true;
}
);
}
私のUserNameがbindiです正しく設定されていて、ViewModelコンストラクタのRefereceとしてPasswordBoxを渡しました。アプリケーションを実行すると、ボタンが無効になるので、コマンドにバインドされていることがわかります。
しかし、私は、私はUserNameとPasswordBoxで物事を入力した後、チェックをされている書いた....そして、決して有効になりませんCanExecuteのdelgateを見たことがない...
は、だから私は間違って何をしたのですか?EDIT:
=====
だから、最後の結果は...このですか?
string _UserName = string.Empty;
public string UserName
{
get
{
return _UserName;
}
set
{
if (value != _UserName)
{
_UserName = value;
RaisePropertyChanged("UserName");
LoginCommand.RaiseCanExecuteChanged();
}
}
}
//For reference the password
PasswordBox _PasswordBox { get; set; }
public DelegateCommand<string> LoginCommand { get; set; }
public LoginViewModel(PasswordBox passwordBox)
{
_PasswordBox = passwordBox;
_PasswordBox.PasswordChanged += delegate(object sender, System.Windows.RoutedEventArgs e)
{
LoginCommand.RaiseCanExecuteChanged();
};
LoginCommand = new DelegateCommand<string>(
(
(str) =>
{
Login(_PasswordBox.Password);
}
),
(usr) =>
{
if (string.IsNullOrEmpty(usr) || string.IsNullOrEmpty(_PasswordBox.Password))
return false;
return true;
}
);
}
CanExecuteデリゲートはどこにありますか?button.Enabled = trueを設定する必要があります。どこでそれをやってるの? – MethodMan
hmm? DelgateCommandの2番目のパラメータがFunc UserNameがnullでないか、空でない、Passwordがnullでない、または空でない場合にのみ、lambada式をtrueに戻しています。または私は間違っていた? –
Jonによると、そこにはPasswordBoxへの参照があるはずではないので、パスワードもプロパティにします。 –