WPF how to set a tooltip to textbox (in style file)
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 TextBox
es 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:
<Style x:Key="DefaultStyle" TargetType="{x:Type TextBox}">
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="VerticalContentAlignment" Valu开发者_开发问答e="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 StackPanel
s etc) to TextBox
(from style file)??
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.
精彩评论