Ninjatalon
Ninjatalon

Reputation: 93

WPF disabling a textbox based on Combobox selecteditem

Using VB.net WPF application i would like to try to bind a combobox selecteditem to the enable property of a text box. I think there is a way to do it using a style/trigger but I don't know how. I can't use the event's property as these to controls are binded inside an ItemsControl and populate dynamically. Any help would be appreciated, Thanks. Here is my xaml:

 <ComboBox x:Name="cmbFood" 
           ItemsSource="{Binding Path=FoodItemList}" 
           SelectedItem="{Binding Path=FoodItem}"
           Width="175"
           >                                                                   
 </ComboBox>

 <xctk:WatermarkTextBox x:Name="txtAmount" 
                        Width="45" 
                        Margin="5,0,0,0" 
                        Text="{Binding Path=Amount}" 
                        Watermark="{Binding Path=wAmount}" 
                        Foreground="Blue"
                        />

Upvotes: 2

Views: 1651

Answers (1)

Chris W.
Chris W.

Reputation: 23280

You can go for DataTrigger so you can just grab the string that is the SelectedValue and if it matches as a condition, tell it to change the IsEnabled state you set to False by default. Basically;

<ComboBox x:Name="cmbFood" 
           ItemsSource="{Binding Path=FoodItemList}" 
           SelectedItem="{Binding Path=FoodItem}"
           Width="175"/>

     <xctk:WatermarkTextBox x:Name="txtAmount" 
                            Width="45" 
                            Margin="5,0,0,0" 
                            Text="{Binding Path=Amount}" 
                            Watermark="{Binding Path=wAmount}" 
                            Foreground="Blue">
                 <xctk:WatermarkTextBox.Style>
                     <Style TargetType="xctk:WatermarkTextBox">
                        <Setter Property="IsEnabled" Value="False" />
                           <Style.Triggers>
                              <DataTrigger Binding="{Binding ElementName=cmbFood, Path=SelectedValue}"
                                                             Value="Whatever-SelectedItem-Changes-The-IsEnabled">
                                 <Setter Property="IsEnabled" Value="True" />
                              </DataTrigger>
                           </Style.Triggers>
                    </Style>
                </xctk:WatermarkTextBox.Style>
     </xctk:WatermarkTextBox>

Hope this helps, cheers.

Upvotes: 2

Related Questions