有没有办法在绑定中检查SelectedIndex属性?

编程入门 行业动态 更新时间:2024-10-28 08:29:31
本文介绍了有没有办法在绑定中检查SelectedIndex属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望根据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属性?

本文发布于:2023-11-03 14:25:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有办法   绑定   属性   SelectedIndex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!