文本框 - 在wpf中编辑样式

时间:2013-10-15 04:11:09

标签: wpf wpf-controls wpf-4.0

我有一个像“文本块”这样的“文本框”。我在双击文本框时启用了编辑功能。现在,我希望在双击文本框时突出显示带有某种颜色的文本框边框。我只需要在代码中应用样式。我该怎么办?我尝试了厚度。但我想要一些整洁干净的东西。

我已经给出了我尝试过的代码。

textBox.IsReadOnly = false;
textBox.SelectAll();
textBox.BorderThickness = new Thickness(1);
你能在这帮我吗?

1 个答案:

答案 0 :(得分:1)

看起来与此类似:EventTrigger with Setter in WPF?

您需要使用EventTrigger来仅使用XAML获得所需的功能。请注意,为了使其工作,您应该将BorderThickness的值更改为非1.的值。如果它是1(默认值),它将显示标准的3d边框。

         <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>