1

我有一个文本框,可以引导用户输入文本进行搜索。

<TextBox  Background="White" 
          Text="{Binding Path=Selected, UpdateSourceTrigger=PropertyChanged}"                  
          TextChanged="textboxsearch_TextChanged"
          Grid.Column="4"  Margin="0,0,11,10" Height="22" Grid.ColumnSpan="2">
    <TextBox.Style>
    <Style TargetType="TextBox">
        <Style.Triggers>
            <Trigger Property="Text" Value="">
                <Setter Property="Foreground" Value="{StaticResource SearchHint}"/>
            </Trigger>
        </Style.Triggers>
        <Setter Property="VerticalAlignment" Value="Bottom"/>
    </Style>
    </TextBox.Style>
</TextBox>

风格:

<VisualBrush x:Key="SearchHint" TileMode="None" Stretch="None" AlignmentX="Center">
    <VisualBrush.Transform>
    <TranslateTransform X="5" Y="0" />
</VisualBrush.Transform>
<VisualBrush.Visual>
    <Grid>
    <TextBlock FontStyle="Italic"
           Foreground="Black"  
                       Background="Black" 
                       Text="Enter search text…" />
    </Grid>
</VisualBrush.Visual>
</VisualBrush>

为什么我运行程序时 Text="Enter search text..." 不可见?谢谢

4

2 回答 2

0

看风格:

 <TextBlock FontStyle="Italic"
            Foreground="Black"  
            Background="Black" Text="Enter search text…" />

您的前景色和背景色是相同的。尝试将前景更改为白色?

于 2013-07-19T19:34:32.703 回答
0

您不能将提示放在Foreground画笔中,因为那只会在 TextBox 中绘制文本。改用Background

<TextBox Text="{Binding Path=Selected, UpdateSourceTrigger=PropertyChanged}"
         TextChanged="textboxsearch_TextChanged"
         Grid.Column="4" Margin="0,0,11,10" Height="22" Grid.ColumnSpan="2">
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <Trigger Property="Text" Value="">
                    <!-- here -->
                    <Setter Property="Background" Value="{StaticResource SearchHint}"/>
                </Trigger>
            </Style.Triggers>
            <Setter Property="VerticalAlignment" Value="Bottom"/>
        </Style>
    </TextBox.Style>
</TextBox>

并使用这个刷子:

<VisualBrush x:Key="SearchHint" TileMode="None" Stretch="None" AlignmentX="Center">
    <VisualBrush.Transform>
        <TranslateTransform X="5" Y="0" />
    </VisualBrush.Transform>
    <VisualBrush.Visual>
        <Grid>
            <TextBlock FontStyle="Italic" Text="Enter search text…"
                        Foreground="Black" Background="White"/>
        </Grid>
    </VisualBrush.Visual>
</VisualBrush>
于 2013-07-21T21:28:26.590 回答