Wpf 无法在样式中设置工具提示内容

Wpf 无法在样式中设置工具提示内容,wpf,xaml,Wpf,Xaml,我试图以样式设置工具提示内容属性。但工具提示文本显示为System.Windows.Style。有人能帮我做错事吗 <TextBlock HorizontalAlignment="Left" Margin="149,45,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="29" Width="121"> <TextBlock.ToolTip>

我试图以样式设置工具提示内容属性。但工具提示文本显示为System.Windows.Style。有人能帮我做错事吗

<TextBlock HorizontalAlignment="Left" Margin="149,45,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="29" Width="121">
        <TextBlock.ToolTip>
            <Style TargetType="{x:Type ToolTip}">
                <Setter Property="Content" Value="ToolTip Test"/>
                <Setter Property="Foreground" Value="Red"/>
                <Setter Property="Foreground" Value="White"/>
            </Style>
        </TextBlock.ToolTip>
    </TextBlock>


您必须将
工具提示
分配给
文本框.工具提示
属性,然后将
样式
分配给
工具提示.样式

<TextBlock HorizontalAlignment="Left" Text="TextBlock">
  <TextBlock.ToolTip>
    <ToolTip>
      <ToolTip.Style>
        <Style TargetType="{x:Type ToolTip}">
          <Setter Property="Content" Value="ToolTip Test" />
          <Setter Property="Foreground" Value="Red" />
          <Setter Property="Foreground" Value="White" />
        </Style>
      </ToolTip.Style>
    </ToolTip>
  </TextBlock.ToolTip>
</TextBlock>


直接设置
FrameworkElement.ToolTip
时,对象将隐式包装到
工具提示中。由于
Style
不是
FrameworkElement
并且无法呈现,因此
ContentControl
ToolTip
)调用内容上的
对象。ToString
(在您的例子中是
Style
)默认情况下,它将完全限定的类型名称返回为
字符串。

您必须将
工具提示
分配给
文本框.ToolTip
属性,然后将
样式
分配给
工具提示.Style

<TextBlock HorizontalAlignment="Left" Text="TextBlock">
  <TextBlock.ToolTip>
    <ToolTip>
      <ToolTip.Style>
        <Style TargetType="{x:Type ToolTip}">
          <Setter Property="Content" Value="ToolTip Test" />
          <Setter Property="Foreground" Value="Red" />
          <Setter Property="Foreground" Value="White" />
        </Style>
      </ToolTip.Style>
    </ToolTip>
  </TextBlock.ToolTip>
</TextBlock>


直接设置
FrameworkElement.ToolTip
时,对象将隐式包装到
工具提示中。由于
Style
不是
FrameworkElement
且无法呈现,因此
ContentControl
ToolTip
)调用内容上的
object.ToString
(在您的例子中是
Style
),默认情况下返回完全限定的类型名为
string

出于好奇,你为什么选择这样做?是否有默认的工具提示值?但是为什么你要在文本框中设置而不是全局设置呢?我只想根据一些条件显示工具提示。为了简化我的问题,我加上了上面的例子。出于好奇,你为什么会选择这样做?是否有默认的工具提示值?但是为什么你要在文本框中设置而不是全局设置呢?我只想根据一些条件显示工具提示。为了简化我的问题,我添加了上面的例子。谢谢,@BionicCode。感谢您指出问题并给出详细解释。谢谢,@BionicCode。用于指出问题并进行详细解释。