Silverlight工具提示样式ContentPresenter文本包装

Silverlight工具提示样式ContentPresenter文本包装,silverlight,styles,tooltip,word-wrap,contentpresenter,Silverlight,Styles,Tooltip,Word Wrap,Contentpresenter,我想创建Silverlight工具提示样式,但不想使用textblock,因为工具提示内容可能是图像或其他内容。所以我使用的是ContentPresenter。我的问题是当内容是文本时如何设置MaxWidth和强制文本换行。这就是我到目前为止所做的: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.mic

我想创建Silverlight工具提示样式,但不想使用textblock,因为工具提示内容可能是图像或其他内容。所以我使用的是ContentPresenter。我的问题是当内容是文本时如何设置MaxWidth和强制文本换行。这就是我到目前为止所做的:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
    xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit">

    <Style TargetType="ToolTip">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToolTip">
                    <Border BorderBrush="DimGray" BorderThickness="1" CornerRadius="5" Background="WhiteSmoke" Opacity="0.8"
                        HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,10,0,-5" Width="Auto">
                        <Border.Effect>
                            <DropShadowEffect BlurRadius="16" ShadowDepth="8" Direction="-45" Color="Black" Opacity="0.6"/>
                        </Border.Effect>
                        <Grid>
                            <ContentPresenter Content="{TemplateBinding Content}" Margin="3"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

我找到了一种方法: 创建两种工具提示样式-一种用于文本,另一种用于其他所有内容,如下所示:

<Style x:Key="TooltipStyleForText" TargetType="ToolTip">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToolTip">
                <Border BorderBrush="DimGray" BorderThickness="1" CornerRadius="5" Background="WhiteSmoke" Opacity="0.8"
                    HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,10,0,-5" Width="Auto">
                    <Border.Effect>
                        <DropShadowEffect BlurRadius="16" ShadowDepth="8" Direction="-45" Color="Black" Opacity="0.6"/>
                    </Border.Effect>
                    <Grid>
                        <TextBlock Text="{TemplateBinding Content}" MaxWidth="400" TextWrapping="Wrap" Margin="3"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="TooltipStyleForOtherStuffThanText" TargetType="ToolTip">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToolTip">
                <Border BorderBrush="DimGray" BorderThickness="1" CornerRadius="5" Background="WhiteSmoke" Opacity="0.8"
                    HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,10,0,-5" Width="Auto">
                    <Border.Effect>
                        <DropShadowEffect BlurRadius="16" ShadowDepth="8" Direction="-45" Color="Black" Opacity="0.6"/>
                    </Border.Effect>
                    <Grid>
                        <ContentPresenter Content="{TemplateBinding Content}" Margin="3"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<TextBlock Text="something">
    <ToolTipService.ToolTip>
        <ToolTip Style="{StaticResource TooltipStyleForText}">
            <TextBlock Text="Some text"/>
        </ToolTip>
    </ToolTipService.ToolTip>
</TextBlock>

当我们使用TextBlock时,将其称为工具提示样式,如下所示:

<Style x:Key="TooltipStyleForText" TargetType="ToolTip">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToolTip">
                <Border BorderBrush="DimGray" BorderThickness="1" CornerRadius="5" Background="WhiteSmoke" Opacity="0.8"
                    HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,10,0,-5" Width="Auto">
                    <Border.Effect>
                        <DropShadowEffect BlurRadius="16" ShadowDepth="8" Direction="-45" Color="Black" Opacity="0.6"/>
                    </Border.Effect>
                    <Grid>
                        <TextBlock Text="{TemplateBinding Content}" MaxWidth="400" TextWrapping="Wrap" Margin="3"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="TooltipStyleForOtherStuffThanText" TargetType="ToolTip">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToolTip">
                <Border BorderBrush="DimGray" BorderThickness="1" CornerRadius="5" Background="WhiteSmoke" Opacity="0.8"
                    HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,10,0,-5" Width="Auto">
                    <Border.Effect>
                        <DropShadowEffect BlurRadius="16" ShadowDepth="8" Direction="-45" Color="Black" Opacity="0.6"/>
                    </Border.Effect>
                    <Grid>
                        <ContentPresenter Content="{TemplateBinding Content}" Margin="3"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<TextBlock Text="something">
    <ToolTipService.ToolTip>
        <ToolTip Style="{StaticResource TooltipStyleForText}">
            <TextBlock Text="Some text"/>
        </ToolTip>
    </ToolTipService.ToolTip>
</TextBlock>

这可能不是最好的解决方案,因为我们有两种样式,所以我们需要为一个简单的文本块指定写几行。
我希望有人有更好的解决方案。

我们需要做同样的事情,并尝试了一些方法,然后才开始使用以下解决方案。问题是,当ToolTip.Content是字符串时,您需要使用文本块显示工具提示。如果ToolTip.Content不是字符串,则需要使用ContentPresenter。因此,我们最终定义了一个同时具有这两个属性的ControlTemplate,并根据ToolTip.Content的类型显示/隐藏相应的控件模板。此方法应用于应用程序中的所有工具提示:

以下转换器类有助于实现这一点:

public class TooltipContentVisibilityConverter : IValueConverter
{
    public Visibility VisibilityWhenToolTipContentIsAString { get; set; }
    public Visibility VisibilityWhenToolTipContentIsNotAString { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var toolTip = value as ToolTip;
        if (toolTip != null && toolTip.Content is string)
        {
            return VisibilityWhenToolTipContentIsAString;
        }

        return VisibilityWhenToolTipContentIsNotAString;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
然后在App.xaml中定义以下内容

<Converter:TooltipContentVisibilityConverter VisibilityWhenToolTipContentIsAString="Visible" VisibilityWhenToolTipContentIsNotAString="Collapsed" x:Key="tooltipStringContentVisibilityConverter" />
<Converter:TooltipContentVisibilityConverter VisibilityWhenToolTipContentIsAString="Collapsed" VisibilityWhenToolTipContentIsNotAString="Visible" x:Key="tooltipNonStringContentVisibilityConverter" />
    <Style TargetType="ToolTip">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="ToolTip">
        <Border CornerRadius="0" Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="1" Padding="4">
          <StackPanel Orientation="Vertical">
            <TextBlock Text="{TemplateBinding Content}" TextWrapping="Wrap" MaxWidth="300" 
                       Visibility="{Binding RelativeSource={RelativeSource AncestorType=ToolTip},Converter={StaticResource tooltipStringContentVisibilityConverter}}"/>
            <ContentPresenter Content="{TemplateBinding Content}" 
                              Visibility="{Binding RelativeSource={RelativeSource AncestorType=ToolTip},Converter={StaticResource tooltipNonStringContentVisibilityConverter}}"/>
          </StackPanel>
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
    </Style>

解决这个问题似乎有点尴尬,但它确实有效,我们还没有找到更好的解决方案