Accomplish with Triggers the job of events from a TextBox in WPF
Hi i am trying to put what i am doing in the events from Code Behind in Triggers of a Style who is being applied in a TextBox. What i want is to prevent using the Code Behind to do that and use Triggers. Is there any way to accomplish this?
My Textbox
<TextBox Name="myTextBox" Style="{StaticResource txtBoxStyle}"/>
My style
<Style x:Key="txtBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="Silver"/>
<Setter Prope开发者_Python百科rty="MaxLength" Value="6"/>
</Style>
The Events from Code Behind
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;
}
}
Thanks in advance!
Edit: Using triggers for this logic is not a good idea and will in all likelihood fail because of its self-referencing (causes a stack-overflow), value converters are also a bit problematic. To encapsulate complex logic and apply it via a Style use an attached behavior.
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsFocused, RelativeSource={RelativeSource Self}}" Value="False"/>
<Condition Binding="{Binding Text, RelativeSource={RelativeSource Self}}" Value=""/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Text" Value="000000"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsFocused, RelativeSource={RelativeSource Self}}" Value="True"/>
<Condition Binding="{Binding Text, RelativeSource={RelativeSource Self}}" Value="000000"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Text" Value=""/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<Trigger Property="Text" Value="000000">
<Setter Property="Foreground" Value="Silver"/>
</Trigger>
</Style.Triggers>
<!-- If (Text != "000000") Foreground = Brushes.Black -->
<Setter Property="Foreground" Value="Black"/>
</Style>
The logic which sets the visibility of the button should not be handled in this style but by the button. The key-down event cannot be translated.
Button style:
<Style TargetType="{x:Type Button}">
<Setter Property="Visibility" Value="Hidden"/> <!-- Normal state -->
<Style.Triggers>
<DataTrigger Binding="{Binding Text.Length, ElementName=myButton}" Value="6">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
精彩评论