Enable text box when combobox item is selected

wpf, wpf-controls

Solution

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.

Edit: Expanded code snippet

Problem

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> ``` I want it in XAML only not in code behind. I dont want to repeat that for every index value –

Original source