选择组合框项时启用文本框 [英] Enable text box when combobox item is selected
问题描述
我想在选择 comboboxitem 时启用文本框.请注意组合框项目未定义,而是我在组合框中使用项目源来获取组合框项目列表.我想在选择组合框项目时更改文本框的属性.
I want to enable the text box when comboboxitem is selected . note the combobox item is not defined but rather i have used item source in combox to get the list of combo box items.i want to change the property of a text box when the combox item is selected .
(评论粘贴到原始问题)
(Comment pasted to original question)
<DataTrigger Binding="{Binding ElementName=cmbInstrumentType,
Path=SelectedIndex}"
Value="1" >
<Setter Property="IsEnabled" Value="true" />
<Setter Property="Background" Value="White" />
</DataTrigger>
我只希望它在 XAML 中,而不是在后面的代码中.我不想为每个索引值重复这一点 -
I want it in XAML only not in code behind. I dont want to repeat that for every index value –
推荐答案
尽管 更好 的方法是使用 MVVM 模式并绑定到 ViewModel 中的属性(如 Dabblenl 建议的那样),我想你可以像这样实现你想要的:
Although the better way to do this is to use the MVVM pattern and bind to a property in your ViewModel (as Dabblenl suggested), I think you can achieve what you want like this:
<StackPanel>
<ComboBox ItemsSource="{Binding Items}" Name="cmbInstrumentType"/>
<TextBox>
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=cmbInstrumentType, Path=SelectedItem}" Value="{x:Null}">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</StackPanel>
如果在组合框中没有选择任何项目,这将禁用文本框.
This will disable the textbox if no item is selected in the combobox.
扩展的代码片段
这篇关于选择组合框项时启用文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!