TextBoxStyle {WPF}上的工具提示样式

2 wpf styles

我尝试在textboxstyle上应用工具提示样式在用户控件中.我有的风格:

<UserControl.Resources>

 <!--Style definition-->

</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

ToolTipStyle:

<Style x:Key="ToolTipStyle" TargetType="{x:Type ToolTip}">
    <Setter Property="Width" Value="200"/>
    <Setter Property="Height" Value="100"/>           
</Style>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

TextBoxStyle适用于文本框控件:

    <TextBox Name="tbNick" 
             Text="{Binding Nick, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
             Style="{StaticResource textBoxStyle}"/>
Run Code Online (Sandbox Code Playgroud)

我得到这个编译错误:

{"Style对象不允许影响它所适用的对象的Style属性."}

堆栈跟踪:

在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)at at 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 System .Windows.Application.LoadComponent(Object component,Uri resourceLocator)在c中的Spirit.Views.ShellView.InitializeComponent()中:\ Users\Jan\Documents\Visual Studio 2010\Projects\C#\ Pokec__Messenger\Spirit_MEF\Views\ShellView.xaml:第1行,位于C:\ Users\Jan\Documents\Visual Studio中的Spirit.Views.ShellView..ctor() 2010\Projects\C#\ Pokec__Messenger\Spirit_MEF\Views\ShellView.xaml.cs:第9行

在WPF中不允许在文本框样式上应用工具提示样式?我做错了什么?

同样在WPF中我使用caliburn.micro和MEF,但我认为它不会导致此错误.

Anv*_*aka 10

没有这样的附加属性ToolTip.Style,编译器没有提供错误的信息描述.如果您想要TextBox使用隐式样式的自定义样式:

<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>
Run Code Online (Sandbox Code Playgroud)


bij*_*iju 9

Style对象不允许影响它应用的对象的Style属性.您可能需要在此处查看http://windows-presentation-foundation.com/WPF_Triggers.aspx

检查此代码以设置工具提示样式

<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>
Run Code Online (Sandbox Code Playgroud)