0

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 ?

shahul hameed
  • 151
  • 2
  • 16
  • Don't manipulate UI elements in procedural code in WPF. That's what XAML is for. Use `Style.Triggers` or other regular WPF mechanism. – Federico Berasategui Oct 15 '13 at 04:34
  • Yeah . But i need to manipulate through code for instance – shahul hameed Oct 15 '13 at 04:48
  • @shahulhammed no you don't need that. I have made thousands of WPF UIs and almost never needed to manipulate UI elements in procedural code. Use XAML for that. – Federico Berasategui Oct 15 '13 at 04:55
  • 1
    If you need to change the style of and element at run time it's better if you create those multiple styles in the respective window or in the app.xaml and through code you just set the element style to your intended new style – Santux Oct 15 '13 at 09:06

1 Answers1

1

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>
Community
  • 1
  • 1
lena
  • 1,181
  • 12
  • 36