0

how to Set properties of text box when combobox selection is made . foe example set background and IsEnabled property of text box when a combo box selection is made. I want it Purely in XAML not in code behind. i use MVVM

user1379584
  • 151
  • 2
  • 4
  • 10
  • 1
    This question is similar to: http://stackoverflow.com/questions/2561820/wpf-visibility-of-a-ui-element-based-on-combo-selection –  May 07 '12 at 11:33

2 Answers2

0

How to enable textBox1 only when SelectedItems is 1

<TextBox Height="23" HorizontalAlignment="Left" Margin="246,177,0,0" Name="textBox2" VerticalAlignment="Top" Width="120">
        <TextBox.Style>
            <Style TargetType="{x:Type TextBox}">
                <Setter Property="IsEnabled" Value="False"></Setter>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=comboBox1, Path=SelectedIndex}" Value="1">
                        <Setter Property="Background" Value="Green"></Setter>
                        <Setter Property="IsEnabled" Value="True"></Setter>
                     </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox> 
    <ComboBox Height="22" HorizontalAlignment="Left" Margin="246,119,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />

I think only with XAML you cannot achieve the condition Value ="1" or "3", i.e. a relation in a data trigger more complex than a equality.

For this case you need a converter. This link could help you

How to get DataTemplate.DataTrigger to check for greater than or less than?

Community
  • 1
  • 1
Klaus78
  • 11,648
  • 5
  • 32
  • 28
  • How to have two or more condition in data triggers, or specifying more than one value for the property for example . i want to specify more than 1 value for SelectedIndex – user1379584 May 08 '12 at 11:10
0

You can use a datatrigger for combo's selected object. Have a look to this previous question: WPF Visibility of a UI element based on combo selection

Try to generate a trigger when selecteditem is {x:Null}. For that, you will need to put your controls inside a DataTemplate and the put the trigger in the template's triggers collection.

Here is a sample code (not tested, please check by your own):

<TextBox Height="23" HorizontalAlignment="Left" Margin="246,177,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" IsEnabled" Value="True" /> 

<ComboBox Height="22" HorizontalAlignment="Left" Margin="246,119,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />

<DataTemplate.Triggers> 
     <Trigger SourceName="comboBox1" Property="ComboBox.SelectedItem" Value="{x:Null}"> 
         <Setter TargetName="textbox2" Property="TextBox.IsEnabled" Value="False" />
     </Trigger> 
</DataTemplate.Triggers> 
Community
  • 1
  • 1
  • can we do this – user1379584 May 08 '12 at 12:13
  • looks like you are taking the inverse logic. Why not to check just for SelectedItem==null? –  May 08 '12 at 12:30
  • can we do it using SelectedItem != null – user1379584 May 08 '12 at 14:01
  • I don't think so. Trigger will check against positive equality. So, what you have to do, is to set the default style in your other controls for the case SelectedItem!=null. then create a simple trigger for SelectedItem Value="{x:Null}" –  May 08 '12 at 14:24
  • I want a text box to be enabled when a combo box selection is made else it will be disabled. First when the application runs also the text box should be disabled, and only if combobox selection is made textbox should get enabled. Purely in XAML as i am following MVVM i dont want it in code behind. – user1379584 May 09 '12 at 06:15
  • that's quiet easy and consistent with my recommendation: create a text control with isenabled="true", then create a combobox with selecteditem=null (explicitly) and datatrigger for element=thecombo, property=isselected, value=null with setter element=thetextbox, property=isenabled, value=false. When you start the application, the trigger should be launched and the textbox will be disabled until you select something in the combo. Notice that the trigger is in the combobox. –  May 09 '12 at 06:40
  • Unfortunately I don't have to much time to try by myself a complete code, I have added a code portion to my answer, please check. It is important to put the controls in a datatemplate. –  May 09 '12 at 08:25
  • Can we have condition like Selected Index is greater than 0 – user1379584 May 09 '12 at 11:50
  • no, not possible with pure xaml: http://stackoverflow.com/questions/793926/how-to-get-datatemplate-datatrigger-to-check-for-greater-than-or-less-than –  May 09 '12 at 12:40