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

时间:2011-03-15 00:18:06

标签: wpf xaml styles multidatatrigger

我搜索并发现我无法直接在setter.value中设置工具提示(在style.xaml文件中)。但是,我可以使用静态资源来设置工具提示。

我的问题是,因为我需要为工具提示提供动态文本,所以我不能使用静态资源。我该怎么做?

这是我的例子。

    <Style x:Key="ErrorStyleRadius" TargetType="{x:Type FrameworkElement}">
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding (Validation.HasError), RelativeSource={RelativeSource Mode=Self}}" Value="True"/>
                <Condition Binding="{Binding (Validation.Errors), RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource IsError}}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <MultiDataTrigger.Setters>
                <Setter Property="ToolTip">
                    <Setter.Value>
                        <ToolTip>
                            <Label Content="{Binding somePropertyHere}"/>
                        </ToolTip>
                    </Setter.Value>
                </Setter>
                <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ErrorControlTemplateRadiusError}"/>
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>

        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding (Validation.HasError), RelativeSource={RelativeSource Mode=Self}}" Value="True"/>
                <Condition Binding="{Binding (Validation.Errors), RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource IsWarning}}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <MultiDataTrigger.Setters>
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors), Converter={StaticResource ValMsg}}"/>
                <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ErrorControlTemplateRadiusWarning}"/>
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

用法,我可以用作

<textbox style={staticresource ErrorStyleRadius} text={bind name, validationOnDataError=true}/>

2 个答案:

答案 0 :(得分:3)

你为什么不试试这个 -

<ToolTip x:Key="MyToolTip"
         DataContext={Binding PlacementTarget, RelativeSource={RelativeSource Self}}>
  <Label Content="{Binding Text}"/>
</ToolTip>

<Style x:Key="ErrorStyleRadius" TargetType="{x:Type FrameworkElement}">
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding (Validation.HasError), RelativeSource={RelativeSource Mode=Self}}" Value="True"/>
                <Condition Binding="{Binding (Validation.Errors), RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource IsError}}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <MultiDataTrigger.Setters>
                <Setter Property="ToolTip" Value={StaticResource MyToolTip}>
                </Setter>
                <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ErrorControlTemplateRadiusError}"/>
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

您可以在StaticResource中提供属性名称。它将动态更新工具提示。

答案 1 :(得分:1)

如果要将文本设置为Validation.Error,也可以从样式中设置工具提示 -

<Trigger Property="Validation.HasError" Value="True">
       <Setter Property="ToolTip"
               Value="{Binding RelativeSource={RelativeSource Self},
                       Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>