Null被视为TextBox WPF样式中的空字符串. [英] Null is considered empty string in style for TextBox WPF.

查看:435
本文介绍了Null被视为TextBox WPF样式中的空字符串.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的样式代码.

This is my style code.

<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.

<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

<Condition Property="Text"  Value="{x:Null}" />



每当我的CustomTitleText属性为null时,它将不起作用.当我将样式触发条件更改为空字符串



It wont work whenever my CustomTitleText property is null, When I changed my style trigger condition to the empty string

<Condition Property="Text"  Value="" />

对于null来说效果很好因为它认为null为空字符串,但我不希望这样.我不明白为什么在触发条件下不将null视为null.

我很期待您的建议,在此先谢谢您.

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.

推荐答案

尝试从不同的角度来理解它:用户不能在文本框中输入null值:当他输入一些文本然后将其删除时,该文本框中包含一个空字符串,而不是null.您只能通过代码设置空值.
顺便说一句,Oracle也采用了这种观点:当您定义文本列并要求其不为null时,则既不允许null也不允许空字符串.
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.


这篇关于Null被视为TextBox WPF样式中的空字符串.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆