ControlTempate wpf 文本框不显示文本和触发器属性 [英] ControlTempate wpf Textbox not showing text and trigger properties
问题描述
我正在继续学习 C# WPF 中的一个小项目.我目前的情况是我想在用户输入发生错误时更改 TextBox
的边框,例如无法将其输入解析为十进制数.
I am continuing my learning with a small project in C# WPF. My current situation is I want to make a border of a TextBox
change when an error occurs on a user input eg can not parse their input into a decimal number.
经过一些阅读,我使用了一个控件模板,XAML 在下面,但是当我运行项目时,TextBox
使用控件模板 (textBox1) 不显示任何文本,我看不出我做错了什么.有人可以帮忙吗?
After some reading up I have used a control template, the XAML is below, but when I run the project the TextBox
using the control template (textBox1) does not show any text and I can not see what I have done wrong. Can any one help please?
此外,我在学习时使用 IsMouseOver
属性触发更改,但在我的项目中,我想触发一个错误属性,因此在我看来我需要添加到我的 TextBox
将属性 IsError
控制为 bool 并在我的代码后面,当我测试用户输入并且它失败 Parse 我会设置 TextBox
属性 IsError
为 true,这将触发 ControlTemplate
更改.但是,这可以做到还是有更稳定的方法来做到这一点?谢谢.
Also I am triggering the change with a IsMouseOver
property just while I learn, but in my project I want to trigger off an error property so in my mind I would need to add to my TextBox
control a property IsError
as a bool and behind my code when I test the user input and it fails the Parse I would set the TextBox
property IsError
to true and that would trigger the ControlTemplate
change. However, can this be done or is there a more standed way to do this?
Thanks.
XAML
<Window x:Class="TestContentStyle.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="StyleTriggersSample" Height="100" Width="300">
<Window.Resources>
<!--A ControlTemplate for textbox including error-->
<ControlTemplate TargetType ="TextBox" x:Key="OnError">
<TextBox Name="TextBox"
FontSize="28"
HorizontalAlignment="Center"
VerticalAlignment="Center"
BorderBrush="Silver"
BorderThickness="1"
Height="50"
Width="120"/>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter TargetName="TextBox"
Property="BorderThickness"
Value="5" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid>
<TextBox Text="Tesing1" Margin="146,23,0,0" Name="textBox1" Template="{StaticResource OnError}" />
<TextBox Text="Testing2" Height="23" HorizontalAlignment="Left" Margin="12,23,0,0" Name="Test1" VerticalAlignment="Top" Width="120" />
</Grid>
推荐答案
首先,模板定义了控件的呈现方式.他们定义了他们的整体外观.
First of all, templates define how a control is rendered. They define their whole look.
您告诉 TextBox
使用另一个不同的 TextBox
内部呈现自身.这意味着您在应用中看到的 TextBox
实际上并不是您在 Grid
中定义的带有Testing1"文本的已在没有文本集的 ControlTemplate
中定义.
You're telling the TextBox
to render itself with another different TextBox
inside. That means that the TextBox
you're seeing in your app is not actually the one you've defined inside your Grid
, with the "Testing1" text, but the one you've defined inside the ControlTemplate
that has no text set.
但除此之外,您不需要为此使用全新的 ControlTemplate
.你只需要一个 Style
:
But other than that, you don't need a whole new ControlTemplate
for this. You just need a Style
:
<Window.Resources>
<Style TargetType="TextBox" x:Key="OnError">
<Setter Property="FontSize" Value="28" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="BorderBrush" Value="Silver" />
<Setter Property="Height" Value="50" />
<Setter Property="Width" Value="120" />
<Style.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="BorderThickness"
Value="5" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<TextBox Text="Tesing1" Margin="146,23,0,0" Name="textBox1" Style="{StaticResource OnError}" />
<TextBox Text="Testing2" Height="23" HorizontalAlignment="Left" Margin="12,23,0,0" Name="Test1" VerticalAlignment="Top" Width="120" />
</Grid>
此外,在 WPF 中,您通常不使用 Margin
来创建布局 :P 相反,您应该将 ColumnDefinitions 和 RowDefinitions 添加到您的 Grid
,并使用 Grid.Row
和 Grid.Column
在 TextBox 上附加属性以指定它们在视图中的位置.
Also, in WPF you don't usually use Margin
to create a layout :P Instead, you should add ColumnDefinitions and RowDefinitions to your Grid
, and use the Grid.Row
and Grid.Column
attached properties on your TextBoxes to specify their location in your view.
这篇关于ControlTempate wpf 文本框不显示文本和触发器属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!