如何实现VisualBrush搜索提示

时间:2013-07-19 19:29:19

标签: xaml

我有一个文本框,可以帮助用户输入搜索文本。

<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 =“输入搜索文本...”不可见?              感谢

2 个答案:

答案 0 :(得分:0)

看看风格:

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

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

答案 1 :(得分: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>