shahul hameed
shahul hameed

Reputation: 151

Text box - editing style in wpf

I have a "text box" like "text block". I am enabling editing functionality in double click of the text box . Now i wanted to highlight the text box border with some color on double click of the text box . I need to apply the style in code only . How to i do it ? I tried with thickness. But i want some neat and clean stuff.

I have given the code what i tried.

textBox.IsReadOnly = false;
textBox.SelectAll();
textBox.BorderThickness = new Thickness(1);

Can you help me here ?

Upvotes: 0

Views: 1704

Answers (1)

lena
lena

Reputation: 1191

Looks similar to this: EventTrigger with Setter in WPF?

You need to use EventTrigger to get the functionality you want using only XAML. Notice that to get it work you should change the value of BorderThickness to something that is not 1. If it is 1 (the default value), it will display standard 3d border.

         <TextBox x:Name="tb" Width="150" Height="30" IsReadOnly="True" Text="Double click to type" 
             BorderBrush="Black" BorderThickness="0.99">
            <TextBox.Triggers>
            <EventTrigger RoutedEvent="TextBox.MouseDoubleClick" SourceName="tb">
                <BeginStoryboard>
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Duration="0"
                                   Storyboard.TargetProperty="(TextBox.IsReadOnly)">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <sys:Boolean>False</sys:Boolean>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                    </BeginStoryboard>
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBox.BorderBrush).Color">
                                <EasingColorKeyFrame KeyTime="0:0:0.1" Value="Red"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            <EventTrigger RoutedEvent="TextBox.LostFocus" SourceName="tb">
                <BeginStoryboard>
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Duration="0"
                                   Storyboard.TargetProperty="(TextBox.IsReadOnly)">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <sys:Boolean>True</sys:Boolean>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBox.BorderBrush).Color">
                                <EasingColorKeyFrame KeyTime="0:0:0.1" Value="Black"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
        </TextBox.Triggers>                
    </TextBox>

Upvotes: 1

Related Questions