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

标签 wpf styles

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

<UserControl.Resources>

 <!--Style definition-->

</UserControl.Resources>

工具提示样式:
<Style x:Key="ToolTipStyle" TargetType="{x:Type ToolTip}">
    <Setter Property="Width" Value="200"/>
    <Setter Property="Height" Value="100"/>           
</Style>

文本框样式:
    <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 object is not allowed to affect the Style property of the object to which it applies."}



堆栈跟踪:

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



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

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

最佳答案

不允许 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>

关于wpf - TextBoxStyle {WPF} 上的工具提示样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4417805/

相关文章:

iframe - 如何更改disqus评论的字体

c# - 如何在一个 XAML 文件中使用多个 ControlTemplates?

c# - 将 RichTextBox FlowDocument 保存到图像

css - 如何为 extjs 4 网格中的列设置样式

php - 我的 html 页面中的事件元素出现问题

reactjs - Material ui 样式的单个 spa 集成问题

c# - 来自 XAML 中不同属性的 WPF DataGrid 单元格样式

WPf ListView : Save reorderd column order

wpf - 使用鼠标事件获得 ScrollViewer 的 PanningMode 行为

wpf - 将样式应用于 ListBoxItem 而不影响内部的 ComboboxItem