Попробуйте следующий код для проверки текста в текстовом поле:
<TextBox x:Uid="txtagevals" x:Name="txtAge" Height="25" Width="80">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Red"/>
<Style.Triggers>
<Trigger Property="Text" Value="18">
<Setter Property="Background" Value="Green"/>
</Trigger>
<Trigger Property="Text" Value="">
<Setter Property="Background" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
В своем коде вы можете сделать это
<TextBox x:Uid="txtagevals" x:Name="txtAge" Height="25" Width="80" BorderThickness="1" BorderBrush="Black">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Red"/>
</Style>
</TextBox.Style>
<TextBox.Template>
<ControlTemplate x:Uid ="txtagevals" TargetType="{x:Type TextBox}">
<Border Background="{TemplateBinding Background}"
BorderBrush="Black" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5">
<ScrollViewer x:Name="PART_ContentHost"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Text" Value="18">
<Setter Property="Background" Value="Green"/>
</Trigger>
<Trigger Property="Text" Value="">
<Setter Property="Background" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</TextBox.Template>
</TextBox>
Вы можете привязать gridview, как показано ниже, у меня все работает нормально:
<ListView Name="lstTest">
<ListView.View>
<GridView>
<GridViewColumn Header="" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox x:Uid="txtagevals" x:Name="txtAge" Height="25" Width="80" BorderThickness="1" BorderBrush="Black" Text="{Binding Path=Age}">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Red"/>
</Style>
</TextBox.Style>
<TextBox.Template>
<ControlTemplate x:Uid ="txtagevals" TargetType="{x:Type TextBox}">
<Border Background="{TemplateBinding Background}"
BorderBrush="Black" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5">
<ScrollViewer x:Name="PART_ContentHost"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Text" Value="18">
<Setter Property="Background" Value="Green"/>
</Trigger>
<Trigger Property="Text" Value="">
<Setter Property="Background" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</TextBox.Template>
</TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>