我尝试在textboxstyle上应用工具提示样式在用户控件中 . 我有的风格:
<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适用于文本框控件:
<TextBox Name="tbNick"
Text="{Binding Nick, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
Style="{StaticResource textBoxStyle}"/>
我得到这个编译错误:
{“不允许样式对象影响它所适用的对象的Style属性 . ”}
堆栈跟踪:
at System.Windows.Markup.XamlReader.RewrapException(Exception e,IXamlLineInfo lineInfo,Uri baseUri)在System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader,IXamlObjectWriterFactory writerFactory,Boolean skipJournaledProperties,Object rootObject,XamlObjectWriterSettings settings,Uri baseUri)在System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader,Boolean skipJournaledProperties,Object rootObject,XamlAccessLevel accessLevel,Uri baseUri)中的System.Windows.Markup.XamlReader.LoadBaml(Stream stream,ParserContext parserContext,Object parent,Boolean closeStream)at at System.Windows.Application.LoadComponent(对象组件,Uri resourceLocator)位于c:\ Users \ Jan \ Documents \ Visual Studio 2010 \ Projects \ C#\ Pokec__Messenger \ Spirit_MEF \ Views \ ShellView中的Spirit.Views.ShellView.InitializeComponent() . xaml:第1行,位于C:\ Users \ Jan \ Documents \ Visual Studio 2010 \ Projects \ C#\ Pokec__Messenger \ Spirit_MEF \ Views \ ShellView.xaml.cs:第9行中的Spirit.Views.ShellView..ctor()
在WPF中不允许在文本框样式上应用工具提示样式?我做错了什么?
同样在WPF中我使用caliburn.micro和MEF,但我认为它不会导致此错误 .
2 回答
Style对象不允许影响它应用的对象的Style属性 . 你可能要看这里http://windows-presentation-foundation.com/WPF_Triggers.aspx
检查此代码以设置工具提示样式
没有
ToolTip.Style
这样的附加属性,编译器没有提供错误的信息描述 . 如果要为TextBox
使用隐式样式设置自定义样式: