使用 MultiBinding 绑定元素

发布于 2024-08-06 16:04:20 字数 702 浏览 14 评论 0 原文

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

我希望仅当两个字段都包含值时才启用“确定”按钮。

我有一个转换器,可以检查所有字符串是否为空或为空。

我在 Convert 方法的第一行放置了一个断点,只有当 MenuItem 初始化时,它才会停止,即当我更改文本时,它不会停止。

下面的示例效果很好,问题是当我更改文本时不会触发多重绑定;它仅在初始化表单时绑定:

<!--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" 的选项。

任何想法?

I have a login form that contains a username textbox and a password box.

I want the ok button to be enabled only when both the fields contain a value.

I have a converter that check for all the strings if they're null or empty.

I placed a breakpoint on the first line of the Convert method, and it stops only when the MenuItem initializes, afterwords, i.e. when I change the text it doesn't.

The following example works good, the problem is that the multibinding is not triggered when i change the text; it's only bound when initializing the form:

<!--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>

I think the issue is that you don't get notified when the remote binding source is changed (e.g. there is no an option to set UpdateTargetTrigger="PropertyChanged".

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏日落 2024-08-13 16:04:20

我建议你研究一下命令绑定。命令可以根据某些条件(即用户名和密码不为空)自动启用或禁用您的登录按钮。

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>

注册命令

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

在 XAML 中或在后面的代码中

public LoginWindow()
{
    InitializeComponent();

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

更多阅读 此处

I would suggest you look into command binding. A command can enable or disable your Login button automatically depending on some condition (ie. user name and password is not empty).

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 command binding will look something like this

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

To register the command in XAML

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

Or in code behind

public LoginWindow()
{
    InitializeComponent();

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

More readigin here.

老旧海报 2024-08-13 16:04:20
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
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
小帐篷 2024-08-13 16:04:20

尝试将 UpdateSourceTrigger 设置为 PropertyChanged,将 Mode 设置为 TwoWay。这将导致该属性在您键入时更新。但不确定这是否适用于您的转换器。

Try setting the UpdateSourceTrigger to PropertyChanged and the Mode to TwoWay. This will cause the property to be updated as you type. Not sure if this will work with your converter, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文