在Text.Length上触发> 0

时间:2016-05-26 15:49:17

标签: c# wpf xaml mvvm

TextBox>时,尝试使用MultiDataTrigger隐藏TextBox.Length中的水印文字。 0

 <Style.Triggers>    
     <MultiDataTrigger>
         <MultiDataTrigger.Conditions>
              <Condition Binding="{Binding ElementName=SearchTermTextBox, UpdateSourceTrigger=PropertyChanged, Path=Text.Length}"
                                           Value="1"/>

                                <Condition Binding="{Binding IsMouseOver, ElementName=SearchTermTextBox, UpdateSourceTrigger=PropertyChanged}"
                                           Value="true"/>                                                          
                 </MultiDataTrigger.Conditions>
             <Setter Property="Visibility"
                     Value="Collapsed"/>
            </MultiDataTrigger>
     </Style.Triggers>

这显然只适用于text.Length = 1。有没有简单的方法来实现这一点,还是我需要编写IValueConverter的实现?

1 个答案:

答案 0 :(得分:1)

反过来呢?默认情况下,不显示水印,并在Text.Length = 0

时触发显示水印
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        x:Name="MyWindow"
        Title="MainWindow"
        Height="200" Width="400">

    <Window.Resources>
        <Style x:Key="TestTextStyle" TargetType="{x:Type TextBlock}">
            <Setter Property="Visibility" Value="Collapsed" />
            <Setter Property="Margin" Value="2" />
            <Setter Property="IsHitTestVisible" Value="False" />
            <Setter Property="Foreground" Value="Silver" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=SearchTermTextBox, UpdateSourceTrigger=PropertyChanged, Path=Text.Length}" Value="0">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <Grid Margin="10">
        <TextBox Name="SearchTermTextBox" />
        <TextBlock Style="{StaticResource TestTextStyle}" Text="Test Watermark" />
    </Grid>
</Window>

enter image description here enter image description here

请确保将初始Visibility=Collapsed设置为样式的一部分,而不是硬编码到<TextBlock />标记,因为DependencyProperty Precedence声明<Tag>中设置的所有属性具有比触发器中设置的优先级更高的优先级,因此该值永远不会改变。