选择组合框项目时启用文本框

use*_*584 5 wpf wpf-controls

我想在选择组合框时启用文本框.注意组合框项目没有定义,而是我在combox中使用了项目源来获取组合框项目的列表.我想在选择combox项目时更改文本框的属性.

(评论粘贴到原始问题)

<DataTrigger Binding="{Binding ElementName=cmbInstrumentType,
              Path=SelectedIndex}" 
              Value="1" >
    <Setter Property="IsEnabled" Value="true" />
    <Setter Property="Background" Value="White" /> 
 </DataTrigger>
Run Code Online (Sandbox Code Playgroud)

我希望它只在XAML中不在代码背后.我不想为每个指数值重复一遍 -

Mat*_*and 8

虽然更好的方法是使用MVVM模式并绑定到ViewModel中的属性(如Dabblenl建议的那样),我认为你可以实现你想要的东西:

    <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>
Run Code Online (Sandbox Code Playgroud)

如果在组合框中未选择任何项目,则将禁用文本框.

编辑:扩展的代码段