上层父控制作为源

时间:2013-03-14 19:31:44

标签: c# wpf

如何在这些情况下将上层控制作为源。 TextBlock需要访问工具提示。

<Style TargetType="{x:Type Control}" x:Key="ErrorTemplate">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="ToolTip">
                <Setter.Value>
                    <Grid Margin="5">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="1" Margin="4, 0,0,0" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ToolTip}, Path=(Validation.Errors)[0].ErrorContent}"></TextBlock>
                    </Grid>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

1 个答案:

答案 0 :(得分:0)

<Setter Property="ToolTip">
                    <Setter.Value>
                        <ToolTip DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}">
                            <Grid Margin="5">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="1" Margin="4, 0,0,0" Text="{Binding Path=(Validation.Errors)[0].ErrorContent}"></TextBlock>
                            </Grid>
                        </ToolTip>
                    </Setter.Value>
                </Setter>