我希望根据ComboBox中Item的选择禁用控件TextBox和PasswordBox。因为我使用TextBox和PasswordBox.so的IsEnabled属性绑定,如何检查绑定中的索引?这是XAML代码:
I want the controls TextBox and PasswordBox to be disabled based on selection of Item in ComboBox.for that I thougt of using IsEnabled Property binding of TextBox and PasswordBox.so,how can I check the index in binding?Here's XAML code:
<ComboBox Name="account_type"SelectionChanged="ComboBox_SelectionChanged"> <ComboBoxItem IsSelected="True">Administrator</ComboBoxItem> <ComboBoxItem>Employee</ComboBoxItem> </ComboBox ... <TextBox Name="credentials" IsEnabled="{some binding}"> <PasswordBox Name="password" IsEnabled="{some binding}">两个控件需要如果选择是Admin.Also,则没有PasswordBox的IsEnabled属性。
The two controls need to be disabled If selection is Admin.Also,there is no IsEnabled property for PasswordBox.
推荐答案假设您要在第一次时禁用控件em>项目在 ComboBox 中被选中,然后绑定到 SelectedIndex 将起作用: Assuming that you want to disable the controls when the first item in the ComboBox is selected, then binding to SelectedIndex will work: <TextBox Name="credentials" IsEnabled="{Binding ElementName=account_type, Path=SelectedIndex}" /> <PasswordBox Name="password" IsEnabled="{Binding ElementName=account_type, Path=SelectedIndex}" />
这是因为绑定将尝试将值转换为布尔值; 0 将转换为 false ,其他任何内容都将转换为 true 。 对于更强大的解决方案,您需要绑定到 SelectedItem ,并且提供转换器将项目的值转换为布尔值。 或者,如果您使用MVVM,您可以使用相关逻辑公开 CredentialEntryEnabled 属性。当帐户类型属性发生更改时,您也会为 CredentialEntryEnabled 属性引发 PropertyChanged 事件。
This is because the binding will try to convert the value to a Boolean; 0 will be converted to false, and anything else will be converted to true. For a more robust solution, you would need to bind to the SelectedItem, and provide a converter to turn the value of the item into a Boolean. Alternatively, if you use MVVM, you could expose a CredentialEntryEnabled property with the relevant logic. When the account type property changed, you would raise the PropertyChanged event for the CredentialEntryEnabled property as well.
更多推荐
有没有办法在绑定中检查SelectedIndex属性?
发布评论