1
您好我想把我正在做的事情从代码后面在正在应用在文本框中的样式的触发器中。我想要的是防止使用代码隐藏来做到这一点,并使用触发器。有什么办法可以做到这一点?完成触发从WPF中的文本框中的事件的作业
我的文本框
<TextBox Name="myTextBox" Style="{StaticResource txtBoxStyle}"/>
我的风格
<Style x:Key="txtBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="Silver"/>
<Setter Property="MaxLength" Value="6"/>
</Style>
从代码的活动
private void myTextBox_LostFocus(object sender, RoutedEventArgs e)
{
if (myTextBox.Text.Length == 0)
myTextBox.Text = "000000";
}
private void myTextBox_GotFocus(object sender, RoutedEventArgs e)
{
if (myTextBox.Text == "000000")
myTextBox.Clear();
}
private void myTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (!(e.Key >= Key.D0 && e.Key <= Key.D9) && e.Key != Key.Back)
e.Handled = true;
}
private void myTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (myTextBox.Text == "000000")
myTextBox.Foreground = Brushes.Silver;
else
{
myTextBox.Foreground = Brushes.Black;
if(myTextBox.Text.Length == 6)
myButton.Visibility = Visibility.Visible;
else
myButton.Visibility = Visibility.Hidden;
}
}
背后感谢提前!
如果我这样做''有默认的setter(出触发器),当文本框获取焦点给我这个异常**文本'Storyboard.TargetProperty路径包含不可动态属性'文本'。**。你能解释我为什么吗? –
Miguel
2011-04-17 18:39:02
对,你不能制作它;无论如何,使用动画并不是必须的,因为我们正在处理一个状态,而不是一个事件。我改变了答案,使用应该工作的setter,并得到相同的结果。 – 2011-04-17 19:11:56
通过这种方式,由于某种原因,我无法在TextBox中编写... – Miguel 2011-04-17 19:31:33