我有一个登录表单,其中包含用户名文本框和密码框。
我希望仅当两个字段都包含值时才启用“确定”按钮。
我有一个转换器,可以检查所有字符串是否为空或为空。
我在 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?
发布评论
评论(3)
我建议你研究一下命令绑定。命令可以根据某些条件(即用户名和密码不为空)自动启用或禁用您的登录按钮。
XAML 命令绑定看起来像这样
注册命令
在 XAML 中或在后面的代码中
更多阅读 此处。
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).
XAML command binding will look something like this
To register the command in XAML
Or in code behind
More readigin here.
尝试将
UpdateSourceTrigger
设置为PropertyChanged
,将Mode
设置为TwoWay
。这将导致该属性在您键入时更新。但不确定这是否适用于您的转换器。Try setting the
UpdateSourceTrigger
toPropertyChanged
and theMode
toTwoWay
. This will cause the property to be updated as you type. Not sure if this will work with your converter, though.