Home > Net >  WPF Style design TextBox Tooltip
WPF Style design TextBox Tooltip

Time:09-13

I would like to style the Tooltip of Textboxes which are used for validation. I'm trying to get a rounded corners Tooltip without sucess. How may I get it from ResourceDictonary?

With the code below, I'm getting the Tooltip text on validation error with a rectangular border. Here is my code:

<ResourceDictionary>
    <!-- Textbox with validation capabilities -->
    <Style x:Key="ValidableTextBox" TargetType="TextBox">
        <Style.Resources>
            <Style x:Key="ToolTip" TargetType="ToolTip">
                <Setter Property="OverridesDefaultStyle" Value="true" />
                <Setter Property="HasDropShadow" Value="True" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ToolTip">
                            <Border Name="Border" CornerRadius="4" BorderThickness="1" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                                <ContentPresenter />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Style.Resources>
        <Setter Property="Height" Value="22"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>

CodePudding user response:

I just realized that BorderBrush property was missing, thanks to Muhammad for he made it working (remove the x:Key="ToolTip"), here is the updated code:

<ResourceDictionary>
    <!-- Textbox with validation capabilities -->
    <Style x:Key="ValidableTextBox" TargetType="TextBox">
        <Style.Resources>
            <Style TargetType="ToolTip">
                <Setter Property="OverridesDefaultStyle" Value="true" />
                <Setter Property="HasDropShadow" Value="True" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ToolTip">
                            <Border Name="Border" CornerRadius="4" BorderThickness="1" BorderBrush="Red" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                                <ContentPresenter />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Style.Resources>
        <Setter Property="Height" Value="22"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>
  • Related