【问题标题】:Silverlight 4. How to set a ToolTip with controls in a ResourceDictionary file with Style definitions?Silverlight 4. 如何在带有样式定义的 ResourceDictionary 文件中设置带有控件的 ToolTip?
【发布时间】:2011-12-15 20:08:45
【问题描述】:

我查看了数十个问答,但还没有找到这个看似简单的需求的答案。

我正在使用 Silverlight 4。我想在具有样式定义的 ResourceDictionary 文件中定义一个带有控件的工具提示。

我的用户控制文件“UC_Activity.xaml”有:

...
<TextBox Style="{StaticResource Style0}" Name="tb_id" />
...

如果我的“Styles.xaml”文件有

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
    <Style x:Key="Style0" TargetType="TextBox">
        <Setter Property="FontSize" Value="12" />
        <Setter Property="FontFamily" Value="Portable User Interface" />
        <Setter Property="ToolTipService.ToolTip" Value="Long tooltip text here. This WORKS, but part of the text ends up out of the screen." />
    </Style>
</ResourceDictionary>

它有效,但我只能将简单的文本显示为工具提示,如果文本很长,它最终会出现在屏幕之外,无法看到。我想要的是这样的:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
    <Style x:Key="Style0" TargetType="TextBox">
        <Setter Property="FontSize" Value="12" />
        <Setter Property="FontFamily" Value="Portable User Interface" />
        <Setter Property="ToolTipService.ToolTip">
            <Setter.Value>
                <StackPanel>
                    <sdk:Label Content="Short text here."/>
                    <TextBlock TextWrapping="Wrap" MaxWidth="200" Text="Long text here. This does NOT WORK." />
                </StackPanel>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

但它不起作用。它构建正常,但在开始执行时会出现异常(“值不在预期范围内。”)。

请问,我该怎么做? 非常感谢。

【问题讨论】:

    标签: xaml silverlight-4.0 styles tooltip


    【解决方案1】:

    我遇到了类似的问题,问题是它需要是一个 DataTemplate,因此每次使用时都会将不同的实例添加到可视化树中。我为此创建了一个附加属性:

    那么就可以这样使用了:

    <Setter Property="ControlsBehaviours:TooltipTemplate.Template">
        <Setter.Value>
                <DataTemplate>
                    <ToolTip Content="tooltip" />
                </DataTemplate>
            </Setter.Value>
        </Setter>
    
    public class TooltipTemplate
    {
        /// <summary>
        /// Template Dependency Property.
        /// </summary>
        public static readonly DependencyProperty TemplateProperty =
            DependencyProperty.RegisterAttached(
                "Template",
                typeof (DataTemplate),
                typeof (TooltipTemplate),
                new PropertyMetadata(new PropertyChangedCallback(TemplateChanged)));
    
        public static void SetTemplate(DependencyObject o, DataTemplate value)
        {
            o.SetValue(TemplateProperty, value);
        }
    
        public static DataTemplate GetTemplate(DependencyObject o)
        {
            return (DataTemplate) o.GetValue(TemplateProperty);
        }
    
        private static void TemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ToolTipService.SetToolTip(d, ((DataTemplate)e.NewValue).LoadContent());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      • 2018-03-26
      相关资源
      最近更新 更多