【问题标题】:Text box - editing style in wpf文本框 - wpf 中的编辑样式
【发布时间】:2023-03-16 03:26:01
【问题描述】:

我有一个“文本框”,例如“文本块”。我通过双击文本框来启用编辑功能。现在我想在双击文本框时用一些颜色突出显示文本框边框。我只需要在代码中应用样式。我该怎么做?我试过厚度。但我想要一些干净整洁的东西。

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

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

你能帮帮我吗?

【问题讨论】:

  • 不要在 WPF 的过程代码中操作 UI 元素。这就是 XAML 的用途。使用Style.Triggers 或其他常规 WPF 机制。
  • 是的。但我需要通过代码进行操作
  • @shahulhammed 不,你不需要那个。我已经制作了数千个 WPF UI,并且几乎不需要在过程代码中操作 UI 元素。为此使用 XAML。
  • 如果您需要在运行时更改元素的样式,最好在相应的窗口或 app.xaml 中创建这些多个样式,并通过代码将元素样式设置为您的预期的新风格

标签: wpf wpf-controls wpf-4.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>

【讨论】:

  • 它工作。但是当我双击文本框时如何更改边框颜色..
  • 这里如何添加边框线颜色故事板?
  • 你需要ColorAnimationUsingKeyFrames,我更新了我的答案,看看这个!
猜你喜欢
  • 2018-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2011-08-14
  • 1970-01-01
相关资源
最近更新 更多