我有一个包含一个用户名文本框和密码框一个登录表单。

我想确定按钮以当两个字段包含的值来才启用。

我有检查所有的字符串,如果他们是空或空的转换器。

我放置一个断点上转换方法的第一行,并停止只有当MenuItem初始化,后记,即,当我更改文本它没有。

以下示例适用好,问题是,当我更改文本的multibinding没有被触发;它仅结合初始化形式时:

<!--The following is placed in the OK button-->
<Button.IsEnabled>
    <MultiBinding Converter="{StaticResource TrueForAllConverter}">
        <Binding ElementName="tbUserName" Path="Text"/>
        <Binding ElementName="tbPassword" Path="Password"/>
    </MultiBinding>
</Button.IsEnabled>

我认为这个问题是你当远程绑定源改变不会得到通知(例如没有设置UpdateTargetTrigger="PropertyChanged"的选项。

任何想法?

有帮助吗?

解决方案

我建议你看看命令绑定。命令可以使能或自动停用登录按钮取决于一些条件(即用户名和密码不为空)。

public static RoutedCommand LoginCommand = new RoutedCommand();

private void CanLoginExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = !string.IsNullOrEmpty(_userInfo.UserName) && !string.IsNullOrEmpty(_userInfo.Password);
    e.Handled = true;
}

private void LoginExecute(object sender, ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Loging in...");
    // Do you login here.
    e.Handled = true;
}

XAML命令绑定将看起来像这样

<TextBox Text="{Binding UserName, UpdateSourceTrigger=PropertyChanged}" />
<TextBox Text="{Binding Password, UpdateSourceTrigger=PropertyChanged}" />
<Button Command="local:LoginWindow.LoginCommand" >Login</Button>

要在XAML注册命令

<Window.CommandBindings>
    <CommandBinding Command="local:LoginWindow.LoginCommand" CanExecute="CanLoginExecute" Executed="LoginExecute" />
</Window.CommandBindings>

或者在后面的代码

public LoginWindow()
{
    InitializeComponent();

    CommandBinding cb = new CommandBinding(LoginCommand, CanLoginExecute, LoginExecute);
    this.CommandBindings.Add(cb);
}

更多readigin 此处

其他提示

Private Sub tb_Changed(sender As Object, e As RoutedEventArgs) _
        Handles tbUsername.TextChanged, _
                tbPassword.PasswordChanged
    btnOk.IsEnabled = tbUsername.Text.Length > 0 _
              AndAlso tbPassword.Password.Length > 0
End Sub

尝试设置到UpdateSourceTriggerPropertyChangedModeTwoWay。这将导致该财产被更新为你键入。不知道这是否会与你的转换工作,虽然。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top