Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my style code.

XML
<Style x:Key="RequiredTextBoxForMultiple" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TextBox}">
                            <Grid>
                                <ScrollViewer SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="PART_ContentHost"/>
                                <TextBlock x:Name="reqText" Text="<multiple>" removed="Pink" Opacity="0.5" Visibility="Hidden" FontStyle="Italic"></TextBlock>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="IsFocused" Value="False" />
                                        <Condition Property="Text"  Value="{x:Null}" />
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Visibility" TargetName="reqText" Value="Visible" />
                                </MultiTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>


& this is my textbox.
XML
<TextBox x:Name="txtBoxCustomTitle" 
 Style="{StaticResource RequiredTextBoxForMultiple}"
 Grid.Column="4" Grid.Row="2"
 Margin="17,3,0,0"
 Text="{Binding CustomTitleText}" MaxLength="80"
 Width="150"
 Grid.ColumnSpan="3"/>


The problem lies in the style's trigger condition
XML
<Condition Property="Text"  Value="{x:Null}" />


It wont work whenever my CustomTitleText property is null, When I changed my style trigger condition to the empty string
XML
<Condition Property="Text"  Value="" />
it worked fine for the null as it considered null as empty string but I dont want that. I don't understand why its not considering null as a null in the trigger condition.

I'm eagerly waiting for your suggestion, Thanks in advance.
Posted

1 solution

Try to understand it from a different point of view: a user cannot enter a null value in a textbox: when he entered some text, and then deletes it, the textbox contains an empty string, not null. Only by code can you set a null value.
By the way, also Oracle takes that point of view: when you define a textual column and require it to be not null, neither null nor empty strings are allowed.
 
Share this answer
 
Comments
Sushil Mate 31-Aug-12 3:06am    
Thanks for your suggestion :) ,Indeed user cant put null value. Actually its part of my implementation. actually I'm setting the null value when textbox values differ & showing multiple style for the textbox so User can understand values are different. when user clicks in the textbox, Visibility style goes away & user can put text in the textbox. hope it clears my requirement.. let me know if you need more explanation.

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