views:

112

answers:

1

Hi I wanted to have nice tooltip for my TextBox, so I started with simple tooltip.

   <TextBox>
 <TextBox.ToolTip>
            <StackPanel>
                <TextBlock>Nice text</TextBlock>
                <TextBlock>Nice text</TextBlock>
            </StackPanel>
        </TextBox.ToolTip>
    </TextBox>

However I have a dozen textboxes and I wanted them all to have the tooltip above. That's why I decided to transfer the code above into a style file.

My style file looks like that

   <Style x:Key="DefaultStyle" TargetType="{x:Type TextBox}">
    <Setter Property="FontFamily" Value="Tahoma"/>
    <Setter Property="FontSize" Value="15"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="TextAlignment" Value="Center"/>
    <Setter Property="TextWrapping" Value="Wrap"/>
    <Setter Property="BorderBrush" Value="#A9C2DE"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Background">

        <Setter.Value>
            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1" >
                <GradientStop Color="#CDE1F7" Offset="0.01"/>
                <GradientStop Color="#DFECFA" Offset="0.8" />
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="ToolTip">
        <Setter.Value>
            <StackPanel>
                <TextBlock Text="Nice toolbox"/>
                <TextBlock Text="Nice tooltip"/>
            </StackPanel>
        </Setter.Value>

    </Setter>
</Style>

However now I get error XAML parse exception. How can I set this kind of tooltip (with stackpanels etc) to textbox (from style file)??

+1  A: 

Dotnet Version < 4

The style looks good but there seems to be an issue with specifing the tooltip directly in the styles value. Declare the ToolTip as a resource and then set it in the style via StaticResource.

<ToolTip x:Key="YourToolTip" >        
    <StackPanel>
        <TextBlock Text="Nice toolbox"/>
        <TextBlock Text="Nice tooltip"/>
    </StackPanel>
</ToolTip>

... Your Style...
<Setter Property="TextBox.ToolTip" Value="{StaticResource YourToolTip}"/>
...

Dotnet Version == 4

If you work with .net4, it's something other. You wrote that you put it into a style file. Do you mean a resource-file? If yes, may be you have not loaded it during runtime. Something like:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/YourStyleFile.xaml"/>
            <ResourceDictionary>
                 <!-- Other local resources -->
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

Another possibility is that you try to use it on another type than TextBox. This does not work because you declared TextBox as target type.

HCL
Thanks it works perfectly
muse
If it works, please accept my answer by clicking on the checkmark on the left of my answer.
HCL