2012-06-12 96 views
1

我想創建一個Silverlight工具提示樣式,但我不想使用文本塊,因爲工具提示內容可能是圖像或其他內容。所以我正在使用ContentPresenter。我的問題是如何設置最大寬度和強制TextWrapping內容是文本時。這是我到目前爲止有:Silverlight工具提示樣式ContentPresenter TextWrapping

<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> 

回答

0

我發現這樣做的一種方法: 創建2個工具提示風格 - 一個是文本,另一個用於一切,就像這樣:

<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,調用它的工具提示風格是這樣的:

<TextBlock Text="something"> 
    <ToolTipService.ToolTip> 
     <ToolTip Style="{StaticResource TooltipStyleForText}"> 
      <TextBlock Text="Some text"/> 
     </ToolTip> 
    </ToolTipService.ToolTip> 
</TextBlock> 

這可能不是最好的解決辦法,因爲我們有兩種風格,所以我們需要指定寫幾行了簡單的TextBlock 。 我希望有人有更好的解決方案。

0

在設置以下解決方案之前,我們需要做同樣的事情並嘗試了幾種方法。問題是,當ToolTip.Content是一個字符串時,您需要使用TextBlock顯示工具提示。當ToolTip.Content不是字符串時,需要使用ContentPresenter。所以我們最終定義了一個ControlTemplate,並且我們根據ToolTip.Content的類型來顯示/隱藏適當的ControlTemplate。這種方法適用於應用中的所有工具提示:

下轉換器類幫助了這一點:

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> 

這似乎有點尷尬的方式來解決這個問題,但它確實工作,我們還沒有找到更好的解決方案呢。