Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a textbox and a combobox. if something selected in the combobox, the textbox should become mandatory. How can i do that...?plz help me

Thanks
Kunjammu
Posted

I would initially set the editbox enabled property to false.
Once the selection changes in the combo, either through binding or via capturing the selectionchanged event you can enable the editbox.
This does not yet imply any validation but it should give a visual clue to the user he is supposed to fill in the edit box.
Then in the ok button handler you can actually create the validation by just coding it.

This is the most simple approach, if you use the model view viewmodel approach there are other methods to do this.

An example of more complex binding :

XML
<Style x:Key="NonEmptyTextBlock" TargetType="TextBlock">
                <Setter Property="Visibility" Value="Visible"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text}" Value="">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True"/>
                            <Condition Binding="{Binding AF}" Value="False"/>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>


The first trigger makes sure that if the textblock,where this style is applied on, is empty, then it is collapsed.
The second trigger is more complex and uses 2 conditions. The first condition is that the listboxitem where this textblock resides in must be selected, the second condition is that the condition AF must be false, this is a variable in my viewmodel. Binding can be a little complex but there are plenty of tutorials and books to teach this.
 
Share this answer
 
v4
Comments
Kunjammu 3-Aug-12 4:01am    
ok.. its a simple approach.Thanks. But i don't want to disable my textbox. When something selected in combobox, an image should show nearer to the textbox indicating that the textbox value is mandatory. is there any way..?
Philip Stuyck 3-Aug-12 4:27am    
yes, just place an image of your liking next to it and set it invisible or collapsed initially. Then when the selection changes you make it visible.
Kunjammu 3-Aug-12 5:19am    
i want to do it using databinding... is there any way?
Philip Stuyck 3-Aug-12 6:06am    
yes via a style and using triggers,
or directly by binding the selectedvalue of the combo to the visiblity of the image and using a valueconverter.
Kunjammu 3-Aug-12 7:03am    
May i get some example on binding method...?
private bool ValidateFields()
{


if(ComboBox.SelectedIndex>=0)
{
write validation for textbox here.

}



}
 
Share this answer
 
Comments
Kunjammu 3-Aug-12 5:18am    
i want to do it using databinding...is there any way?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900