WPF样式文本框

时间:2014-08-05 09:23:00

标签: c# wpf textbox

我正在尝试为我的文本框创建一个样式:

<Style TargetType="{x:Type TextBox}">
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
        <Setter Property="FocusVisualStyle"   Value="{x:Null}" />
        <Setter Property="MinWidth" Value="100" />
        <Setter Property="MinHeight" Value="25" />
        <Setter Property="AllowDrop"  Value="true" />
        <Setter Property="BorderBrush" Value="{DynamicResource CouleurBouton}" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="FontFamily" Value="Helvetica" />
        <Setter Property="FontSize" Value="14" />
        <Setter Property="Foreground" Value="{DynamicResource CouleurTexte}" />
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBoxBase}">
                    <Border Name="Border"
                CornerRadius="7"
                Padding="2"
                BorderThickness="2"
                Background="{TemplateBinding Background}"
                                        BorderBrush="{TemplateBinding BorderBrush}">
                        <ScrollViewer Margin="0"
                        x:Name="PART_ContentHost" />
                    </Border>
                    <ControlTemplate.Triggers>

                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <!-- Mouse Over -->
                                <Condition Property="IsEnabled" Value="True" />
                                <Condition Property="IsMouseOver" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush" Value="{DynamicResource CouleurBoutonHover}"/>
                            <Setter Property="Foreground" Value="{DynamicResource CouleurTexte}" />
                        </MultiTrigger>

                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <!-- Desactivé-->
                                <Condition Property="IsEnabled" Value="False" />
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush" Value="{DynamicResource CouleurBoutonDisabled}"/>
                            <Setter Property="Foreground" Value="{DynamicResource CouleurBoutonDisabled}"/>
                        </MultiTrigger>

                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <!-- Focus-->
                                <Condition Property="IsEnabled" Value="True" />
                                <Condition Property="IsFocused" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush" Value="{DynamicResource CouleurBoutonPressed}"/>
                            <Setter Property="Foreground" Value="{DynamicResource CouleurTexte}" />
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

这种风格符合我的需要,但我想在文本框中自定义“光标”(垂直线)的颜色(在此图中指出:enter image description here

我该怎么做?

谢谢!

1 个答案:

答案 0 :(得分:2)

为所需颜色设置样式的CaretBrush属性

例如

<Setter Property="CaretBrush" Value="Aqua" />