我是WPF的新手....我已经尝试为textbox实现controltemplate和datatriggers ..我想通过datatriggers输入的值不是“18”时更改文本框的背景颜色。 (我希望文本框的controlproperty工作原样)我怎样才能实现它.... 编写的Xaml代码如下:
<TextBox x:Uid="txtagevals" x:Name="txtAge" Height="25" Width="80" Background="Wheat" BorderThickness="1" BorderBrush="Black">
<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>
<DataTrigger Binding="{Binding Age}" Value="18">
<Setter TargetName="" Property="Text" Value="Green" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</TextBox.Template>
</TextBox>
任何人都可以帮助我......谢谢..
答案 0 :(得分:2)
尝试以下代码验证文本框中的文字:
<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>