Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

XML
<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}">


The two controls need to be disabled If selection is Admin.Also,there is no IsEnabled property for PasswordBox.
Posted
Updated 30-Oct-14 10:09am
v2
Comments
Richard Deeming 30-Oct-14 16:41pm    
"Also,there is no IsEnabled property for PasswordBox."

Yes, there is: UIElement.IsEnabledProperty[^]

Since PasswordBox : Control : FrameworkElement : UIElement, any property defined on UIElement will be available on PasswordBox.
Member 11098660 31-Oct-14 2:27am    
yes,you're right.thanks!

1 solution

Assuming that you want to disable the controls when the first item in the ComboBox is selected, then binding to SelectedIndex will work:
XML
<TextBox Name="credentials" IsEnabled="{Binding ElementName=account_type, Path=SelectedIndex}" />
<PasswordBox Name="password" IsEnabled="{Binding ElementName=account_type, Path=SelectedIndex}" />

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900