为文本框控件实现控件模板和数据触发器
我是 WPF 新手....我尝试为文本框实现 controltemplate 和 datatriggers ..当通过数据触发器输入的值不是“18”时,我想更改文本框的背景颜色..(我想要文本框的控制属性按原样工作)我怎样才能实现它...... 编写的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>
有人可以帮助我吗...谢谢..
I'm new to WPF....I have tried implementing controltemplate and datatriggers both for textbox..I want to change the background color of the textbox when the value entered in that is not "18" through datatriggers..(i want the controlproperty for the textbox to work as it is)how can i achieve it....
the Xaml code written is as follows:
<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>
can anybody please help me...Thank you..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试下面的代码来验证文本框中的文本:
在您的代码中,您可以这样做
您可以像下面这样绑定gridview,它对我来说工作正常:
Try below code to validate text in textbox :
In your code, you can do like this
You can bind gridview like below, it's working fine for me :