ToolTip style on TextBoxStyle {WPF}

12,324

Solution 1

A Style object is not allowed to affect the Style property of the object to which it applies. You may have to check here http://windows-presentation-foundation.com/WPF_Triggers.aspx

check this code for setting tooltip style

<Grid>

  <Grid.Resources>

    <Style x:Key="MyTooltip" TargetType="{x:Type ToolTip}">

      <Setter Property = "HorizontalOffset" Value="50"/>

      <Setter Property = "VerticalOffset" Value="50"/>

      <Setter Property = "Background" Value="Orange"/>

      <Setter Property = "Foreground" Value="Red"/>

      <Setter Property = "FontSize" Value="14"/>

      <Setter Property = "FontWeight" Value="Bold"/>

      <Setter Property = "FontFamily" Value="Courier New"/>

    </Style>

  </Grid.Resources>



  <TextBox Margin="10,10,10,10" Height="20">

    Pass over with your Mouse

    <TextBox.ToolTip>

      <ToolTip Style="{StaticResource MyTooltip}">

        <TextBlock>This is the Tooltip</TextBlock>

      </ToolTip>

    </TextBox.ToolTip>

  </TextBox>

</Grid>

Solution 2

There is no such attached property as ToolTip.Style, and compiler gives not quite informative description of the error. If you want to have a custom style for the TextBox use implicit style:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Page.Resources>
      <Style x:Key="textBoxStyle" TargetType="{x:Type TextBox}">
         <Style.Resources>
            <Style x:Key="{x:Type ToolTip}" TargetType="{x:Type ToolTip}">
               <Setter Property="Width" Value="200"/>
               <Setter Property="Height" Value="100"/>
            </Style>
         </Style.Resources>
         <Setter Property="Width" Value="200"/>
         <Setter Property="Height" Value="25"/>
         <Setter Property="FontSize" Value="13"/>
         <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>
   </Page.Resources>
   <Grid>
      <TextBox Name="tbNick" Style="{StaticResource textBoxStyle}" Text="Test" ToolTip="Hey"/>
   </Grid>
</Page>
Share:
12,324
Admin
Author by

Admin

Updated on June 25, 2022

Comments

  • Admin
    Admin almost 2 years

    I try apply tooltip style on textboxstyle In user control. Style I have in:

    <UserControl.Resources>
    
     <!--Style definition-->
    
    </UserControl.Resources>
    

    ToolTipStyle:

    <Style x:Key="ToolTipStyle" TargetType="{x:Type ToolTip}">
        <Setter Property="Width" Value="200"/>
        <Setter Property="Height" Value="100"/>           
    </Style>
    

    TextBoxStyle:

        <Style x:Key="textBoxStyle" TargetType="{x:Type TextBox}">
            <Setter Property="Width" Value="200"/>
            <Setter Property="Height" Value="25"/>
            <Setter Property="FontSize" Value="13"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
    
            <!--Apply toolip style-->
            <Setter Property="ToolTip.Style" Value="{StaticResource ToolTipStyle}"/>
    
    
            <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>
    

    TextBoxStyle apply on textbox constrol:

        <TextBox Name="tbNick" 
                 Text="{Binding Nick, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                 Style="{StaticResource textBoxStyle}"/>
    

    I get this compile error:

    {"Style object is not allowed to affect the Style property of the object to which it applies."}

    StackTrace:

    at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri) at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri) at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream) at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) at Spirit.Views.ShellView.InitializeComponent() in c:\Users\Jan\Documents\Visual Studio 2010\Projects\C#\Pokec__Messenger\Spirit_MEF\Views\ShellView.xaml:line 1 at Spirit.Views.ShellView..ctor() in C:\Users\Jan\Documents\Visual Studio 2010\Projects\C#\Pokec__Messenger\Spirit_MEF\Views\ShellView.xaml.cs:line 9

    Apply tooltip style on textbox style is not allowed in WPF? What I do wrong?

    Also in WPF I use caliburn.micro and MEF, but I think it doesn’t caused this error.