如何在文本框中为空或空时设置提示文本(提示横幅),如果它不为空或为空则禁用?

时间:2021-06-24 20:30:51

I'm new to xaml and not sure how databind works. I'm trying to enable hint text when my text box is null. But I'm not sure how the data bind work to disable it when it's not null.

我是xaml的新手,不知道数据绑定是如何工作的。当我的文本框为空时,我正在尝试启用提示文本。但我不确定数据绑定是如何工作的,当它不为空时禁用它。

Here is a solution I found that works to get my hint text. How do this code to work to disable it if my first name textbox has a value?

这是我发现的一个解决方案,用于获取我的提示文本。如果我的名字文本框有值,这段代码如何禁用它?

<TextBox Grid.Row="1">
    <TextBox.Style>
        <Style TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib">
            <Style.Resources>
                <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
                    <VisualBrush.Visual>
                        <Label Content="First Name" Foreground="LightGray" />
                    </VisualBrush.Visual>
                </VisualBrush>
            </Style.Resources>
            <Style.Triggers>
                <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                    <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                </Trigger>
                <Trigger Property="Text" Value="{x:Null}"> 
                    <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                </Trigger>
                <Trigger Property="IsKeyboardFocused" Value="True">
                    <Setter Property="Background" Value="White" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>

    <TextBox.Template>
        <ControlTemplate>
            <TextBox Text="{Binding SelectedItem.FirstName,UpdateSourceTrigger=PropertyChanged}"/>
        </ControlTemplate>
    </TextBox.Template>
</TextBox>

1 个解决方案

#1


0  

Never mind I figured out.

没关系,我想通了。

I could set the textbox binding like:

我可以设置文本框绑定,如:

<TextBox Text="{Binding SelectedItem.FirstName,
                        UpdateSourceTrigger=PropertyChanged}"
         Grid.Row="1">

I didn't know you could do that if you had an opening and closing <textbox> and </textbox> tags.

如果你有一个打开和关闭的 和 标签,我不知道你能做到这一点。

I thought you could only do it if the textbox has a self-closing tag (<textbox />).

我认为只有文本框具有自动关闭标记( )才能执行此操作。

Sorry for the silly question.

抱歉这个愚蠢的问题。

#1


0  

Never mind I figured out.

没关系,我想通了。

I could set the textbox binding like:

我可以设置文本框绑定,如:

<TextBox Text="{Binding SelectedItem.FirstName,
                        UpdateSourceTrigger=PropertyChanged}"
         Grid.Row="1">

I didn't know you could do that if you had an opening and closing <textbox> and </textbox> tags.

如果你有一个打开和关闭的 和 标签,我不知道你能做到这一点。

I thought you could only do it if the textbox has a self-closing tag (<textbox />).

我认为只有文本框具有自动关闭标记( )才能执行此操作。

Sorry for the silly question.

抱歉这个愚蠢的问题。